Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Object-Oriented Python (retired) Inheritance Intro to Inheritance

dmitriy ignatiev
seal-mask
.a{fill-rule:evenodd;}techdegree
dmitriy ignatiev
Python Web Development Techdegree Student 1,789 Points

Inheritance in python

Hi, try to create a new monster DDD = Monster(color = 'red', adjective = 'jfdhg') as it was shown in video and add a new attribute = 'adjective' wich wasn't add in a code but recieved an error. Please clarify why it could happen i can not see any mistakes in my code

AttributeError: 'Monster' object has no attribute 'adject

1 Answer

To be able to instantiate a Monster object from the Monster class.

Each attribute you pass into the ( ) of Monster() needs to be defined within the Monster class. Does that make sense?

If you watch the video again, pay close attention to what Kenneth does inside this method.

def __init__(self, **kwargs):

Basically to be able to pass in things to Monster like this

goblin = Monster(color='red', adjective='scary')

Both color and adjective have to be defined inside the class which is what Kenneth did inside the init method. :)

Roman Mayer
Roman Mayer
10,925 Points

The whole point of this video IMHO is, that you don't need to pre-define all attributes. When you use

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

in your init, then any attribute can be set during instantiation:

a = Monster(foo = "bar")
>>> a.foo
'bar'