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 Object-Oriented Python Advanced Objects Subclassing Built-ins

What does this do?

What does super().init do

1 Answer

Erlend Dybvik Indrelid
Erlend Dybvik Indrelid
5,843 Points

Hi! super() is used to get access to the methods of the subclass' superclass. This is useful because it stops repetition of code if any of the methods from the superclass is needed in the subclass. super().__init__ is thus used to get access to the superclass' __init__ method. If you are still unsure of what super() does, Realpython does a great job explaining its exact purpose:

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)

As you can see, the Square class inherits from the Rectangle class, and can thus access Rectangle's __init__ method by using super().__init__, followed by the required arguments. Let me know if it is still a bit unclear!