24

I am new to Python and programming in general and try to teach myself some Object-Oriented Python and got this error on my lattest project:

AttributeError: type object 'Goblin' has no attribute 'color'

I have a file to create "Monster" classes and a "Goblin" subclass that extends from the Monster class. When I import both classes the console returns no error

>>>from monster import Goblin
>>>

Even creating an instance works without problems:

>>>Azog = Goblin
>>>

But when I call an attribute of my Goblin class then the console returns the error on top and I don't figure out why. Here is the complete code:

import random

COLORS = ['yellow','red','blue','green']


class Monster:
    min_hit_points = 1
    max_hit_points = 1
    min_experience = 1
    max_experience = 1
    weapon = 'sword'
    sound = 'roar'

    def __init__(self, **kwargs):
        self.hit_points = random.randint(self.min_hitpoints, self.max_hit_points)
        self.experience = random.randint(self.min_experience,  self.max_experience)
        self.color = random.choice(COLORS)

        for key,value in kwargs.items():
            setattr(self, key, value)

    def battlecry(self):
        return self.sound.upper()


class Goblin(Monster):
    max_hit_points = 3
    max_experience = 2
    sound = 'squiek'
0

3 Answers 3

24

You are not creating an instance, but instead referencing the class Goblin itself as indicated by the error:

AttributeError: type object 'Goblin' has no attribute 'color'

Change your line to Azog = Goblin()

Sign up to request clarification or add additional context in comments.

Comments

7

When you assign Azog = Goblin, you aren't instantiating a Goblin. Try Azog = Goblin() instead.

Comments

-1

In

 def __init__(self, **kwargs):
        self.hit_points = random.randint(self.min_hitpoints, self.max_hit_points)

Switch self.min_hitpoints to self.min_hit_points

and of course do: Azog = Goblin()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.