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

I think that the Python course completely lost itself at the OO section

Everything goes so smoothly with the Python course, even if you get stuck after a while in some tasks, you get how everything works and why you are doing something. But all the sudden when you enter the OO section, everything falls apart... Treehouse doesn't take enough time to explain to you why you are doing all the this, super().... At a certain point Kenneth says something about an argument getting lost with a super() call, like... what is going on? Slow down please....

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

Let's break down the super() usage.

  • When one class extends (or sub-classes) another class, all of the parents methods are automatically available in the new subclass.
  • The subclass may override a parent class method by simply defining a new method of the same name. Note that the parent class method is still exists, but only appears hidden by the new subclass method.
  • If the subclass wants to extend the parent class method, then a mechanism is needed to refer to the parent class method separately from the subclass version of the method.
  • The super() function says skip the subclass namespace and look directly to the parent class namespace for a specific method. Think of it as a way to jump up the inheritance hierarchy looking for code.

Sometimes we want to run the parent class method, then do more work:

class SomeClass(ParentClass):
    def __init__(self):
        # run the parent class __init__ first
        super().__init__()
        # then do more stuff
        self.new_attribute = "I'm a subclass"

Sometimes we want to prep data before calling the parent class method. This is likely when the parent class method takes an argument we want to adjust:

class SomeClass(ParentClass):
    def __init__(self):
        # then do prep stuff
        die = D6
        # run the parent class __init__ last
        super().__init__(die_class=die)

These are very simplistic examples, but give the general idea. Post back if you have more questions!!