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.
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
Jeff MudayTreehouse Moderator 27,918 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
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
Thanks a lot now i have got the point :)