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 Inheritance Multiple Superclasses

Multiple Superclass

class Sneaky: sneaky = True

def __init__(self, sneaky=True, *args, **kwargs):
    super().__init__(*args, **kwargs)
    self.sneaky = sneaky

Is it possible to have super() without inheritance since in the above code class Sneaky doesn't seems to be inherited from any other class. Any suggestion will be appreciated :)

1 Answer

Richard Li
Richard Li
9,751 Points

You can check the docs of multiple-inheritance:

https://docs.python.org/3/tutorial/classes.html#multiple-inheritance

For most purposes, in the simplest cases, you can think of the search for attributes inherited from a parent class as depth-first, left-to-right, not searching twice in the same class where there is an overlap in the hierarchy.

and docs of super() https://docs.python.org/3/library/functions.html#super

*There are two typical use cases for super. In a class hierarchy with single inheritance, super can be used to refer to parent classes without naming them explicitly, thus making the code more maintainable. This use closely parallels the use of super in other programming languages.

The second use case is to support cooperative multiple inheritance in a dynamic execution environment. This use case is unique to Python and is not found in statically compiled languages or languages that only support single inheritance. This makes it possible to implement โ€œdiamond diagramsโ€ where multiple base classes implement the same method. Good design dictates that this method have the same calling signature in every case (because the order of calls is determined at runtime, because that order adapts to changes in the class hierarchy, and because that order can include sibling classes that are unknown prior to runtime).*

My understanding is that:

Thief class will check Character-Agile-Sneaky-[object](the super Superclass for all classes) in this order to collect all the attributes.

Now with the init , when the super() is present in each class, python will check the next one until reach the object class (top-level). Agile's init will check Sneaky and Sneaky init will check object.

The video is poorly written in this and I don't know if my understanding is right in this regard so I am still researching on this and feel free to let me know if you think otherwise...