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

Need To understand the super method with no inheritance

in the listed Example in the video there was two classes (Agile and Sneaky) inside attribute.py how used super inside them despite that they did not inherited from any above classes like previous Videos Examples

2 Answers

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,720 Points

I agree with you that this video is not so clear.

Kenneth is demonstrating "Loosely Coupled Code" to call our attention to what is going on with the __super__ method and how if it is not included, you might have unexpected results associated with MRO (method resolution order) with multiple inheritances. If these are left out, args and kwargs might not be in the instantiated object unless these are passed up the "chain" with the __super__ method.

I am not a fan of his example as I think it demonstrates what can go wrong with Object-Oriented Programming. Conceptually, Character is a class, I agree with that. But the idea that Agile and Sneaky are classes, I would not agree -- semantically speaking, Agile and Sneaky are modifiers of the Character object.

Here's a simpler example of __super__. Think about Squares and Rectangles. A Square is a Rectangle with equal length and width.

If we didn't use inheritance, we would duplicate lots of code!

class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width

    def perimeter(self):
        return 2 * self.length + 2 * self.width

class Square:
    def __init__(self, length):
        self.length = length

    def area(self):
        return self.length * self.length

    def perimeter(self):
        return 4 * self.length

If we use inheritance and __super__ we can save lots of code (and testing).

class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width

    def perimeter(self):
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square(Rectangle):
    def __init__(self, length):
        super().__init__(length, length) # note the super is sending these up to the parent class
Craig Stephenson
Craig Stephenson
2,490 Points

I do not see how your example explains the "use of the super class with no [explicit] inheritance". In the example you give, the class Square inherits explicitly from Rectangle. (It is a nice clear example of inheritance and it is easy to understand.) The problem several of us seem to be having with Kenneth's example, is that the init methods of both the attributes classes, i.e. Sneaky and Agile, call super(), but neither of these classes explicitly have a parent class. So I would have thought that the only parent class of Sneaky and Agile is Object, but I doubt that the init method of Object calls setattr to set the attributes for the keyword arguments.

For me what is going on is that the Thief class is defining the order of inheritance, which is then used by the calls to super() in the Agile and Sneaky classes.

Thanks a lot now i have got the point :)