Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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 67,989 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!!