Python Object-Oriented Python Inheritance Super-Duper!

Kohei Ashida
Kohei Ashida
4,881 Points

Why don't you need "self" parameter?

Why don't you need "self" parameter when you call super() like super.init(self, name, **kwargs) instead of super().init(name, **kwargs)?

2 Answers

Steven Parker
Steven Parker
203,995 Points

They system adds the "self" parameter when the call is processed, it's not passed as an explicit argument. This is unrelated to the use of "super", and happens on any class method.

Steven Parker
Steven Parker
203,995 Points

The system's insertion of the "self" argument happens anytime you make a method call, whether it is done inside the same (or another) class or just in the outer code.

Kohei Ashida
Kohei Ashida
4,881 Points

Thanks Steven! Now I understood that the system's insertion happens anytime I make a method call but why don't you need "self" parameter when you define a method in another method like below.

class Sneaky:
    sneaky = True

    def __init__(self, sneaky=True, *args, **kwargs): # "self" parameter needed
        super().__init__(*args, **kwargs) # no "self" parameter needed
        self.sneaky = sneaky
Steven Parker
Steven Parker
203,995 Points

That second line is not a definition, it is calling the other method. The call is where you don't supply a "self" because the system will add it for you.

Kohei Ashida
Kohei Ashida
4,881 Points

Thank you for your explanation! Now I got it that I misunderstood the second line is the same thing as the "def" line.

Kohei Ashida
Kohei Ashida
4,881 Points

Thanks for quick response as always, Steven! So you're saying that the "self" parameter in the method is systematically added when the method itself is written in another method of a class?