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

Python Class Inheritance

Think I might have misunderstood some of the concepts around inheritance.

This isn't related to a specific code challenge or video so I've created an example piece of code below:

class ParentClass:
     def __init__(self, first_name, last_name):
             self.first_name = first_name
             self.last_name = last_name

 class SubClass(ParentClass):
     def __init__(self, age, location):
             super().__init__(self)
             self.age = age
             self.location = location

 my_instance = SubClass(first_name="Joe", second_name="Bloggs", age=20, location="United Kingdom")

So I keep receiving a TypeError:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: __init__() got an unexpected keyword argument 'first_name'

As you can see I've overwritten the init method in SubClass as I wanted to add two new attributes (age, location), but then I've used super(). Shouldn't this allow me to specify first_name and last_name on the instance (my_instance) of SubClass?

Would be really grateful if anyone could offer me some help.

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,468 Points

Great work trying this on your own! The main issue is there isn't any place for the SubClass.__init__ to accept arguments that it would pass on to the ParentClass through the super() call. This where **kwargs is useful.

Rewriting you code (and correcting indentation issues):

>>> class ParentClass:
...      def __init__(self, first_name, last_name):
...              self.first_name = first_name
...              self.last_name = last_name
... 
>>> class SubClass(ParentClass):
...     def __init__(self, age, location, **kwargs):  # added **kwargs
...         super().__init__(**kwargs)  # supplying  **kwargs to parent __init__, do Not include 'self'
...         self.age = age
...         self.location = location
... 
>>> my_instance = SubClass(first_name="Joe", second_name="Bloggs", age=20, location="United Kingdom")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in __init__
TypeError: __init__() got an unexpected keyword argument 'second_name'

# replacing second_name with last_name:
>>> my_instance = SubClass(first_name="Joe", last_name="Bloggs", age=20, location="United Kingdom")
>>> my_instance.__dict__
{'first_name': 'Joe', 'last_name': 'Bloggs', 'age': 20, 'location': 'United Kingdom'}

As when calling other methods, the self is not included when calling __init__

Post back if you have more questions. Good luck!!

Hi Chris, this makes perfect sense. Thanks so much for your help!