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 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!