Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does 'super().__init__()' mean in python 3.x?

What is the difference between these two code samples?

1:

class SubType(type):
    def __init__(cls, name, bases, dct):
        super().__init__(name, bases, dct)

2:

class SubType(type):
    def __init__(cls, name, bases, dct):
        pass
like image 909
user2909276 Avatar asked Oct 23 '13 00:10

user2909276


1 Answers

In python 3.x it means calling the __init__ method of the superclass (i.e. type) (as if it were a method of the current class, SubType, since the current class is a derived of the superclass).

It is the same as calling super(type, self).__init__() in Python 2.x

For example:

class type:
       def __init__(self, a):
           print(a)

 class SubType(type):
       def __init__(self, a):
           super().__init__(a)

>> obj = SubType(2) 
2
>>
like image 186
Mihai Andrei Avatar answered Oct 21 '22 20:10

Mihai Andrei