Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why super().__init__ doesn't have a self-reference? [duplicate]

Why don't we need self reference when we use super().__init__?(such as line 9 down below)

class labourers():
    def __init__(self,name,department,salary):
        self.name = name
        self.department = department
        self.salary = salary

class managers(labourers):
    def __init__(self,name,department,salary,numberofpeople):
        super().__init__(name,department,salary)
        self.numberofpeople = numberofpeople

like image 311
ersanowski Avatar asked Apr 08 '26 15:04

ersanowski


1 Answers

Super's functionality in this case is implemented in the CPython parser. See PEP 3135

Replacing the old usage of super, calls to the next class in the MRO (method resolution order) can be made without explicitly passing the class object (although doing so will still be supported). Every function will have a cell named __class__ that contains the class object that the function is defined in.

The new syntax:

super()

is equivalent to:

super(__class__, <firstarg>)

[...]

While super is not a reserved word, the parser recognizes the use of super in a method definition and only passes in the __class__ cell when this is found. Thus, calling a global alias of super without arguments will not necessarily work.

Emphasis added.

like image 177
sytech Avatar answered Apr 11 '26 05:04

sytech