super().__init__() 、 super(B,self).__init__()
需要注意的是python2、3的super写法稍有不同。
__init__() is a constructor in python, which is "automatically called" when creating an object.
定义类时可以不写init方法,系统会默认创建, 你也可以写一个,让你的类在创建时完成一些“动作”。
If both subclass B and parent class A have written init methods,
Then the init method of A will be overwritten by B. If you want to call A's init method, you need to use super to call it.
Of course, inside B, in addition to using super to call the parent class method, you can also call it using the parent class name, for example:
class B(A): def __init__(self): A.__init__(self) print("B init")
Some people may misunderstand the meaning of "coverage" and think that "coverage" means there is no such thing. Why can it still be called through super?
Covered does not mean that it is gone. The method of A is still there after all, but it needs to be called with super inside B.
例: A里写了一个方法hi(), B继承自A, B里也写了一个方法hi()。 B的对象在外部调用hi(), 就只能调用B里面写的这个hi()。 想通过B调用A的hi(),只能在B内部用super().hi()调用。
class A: def hi(self): print("A hi") class B(A): def hello(self): print("B hello") b = B() b.hi() # B里没有写hi(),这里调用的是继承自A的hi() ------------------------------------------------------------------ class A: def hi(self): print("A hi") class B(A): def hi(self): print("B hi") b = B() b.hi() # 这里调用的就是B自己的hi() ------------------------------------------------------------------ class A: def hi(self): print("A hi") class B(A): def hi(self): super().hi() # 通过super调用父类A的hi() print("B hi") b = B() b.hi() # 这里调用的就是B里面的hi()
One difference between Python3.x and Python2.x: Python 3 can use super().xxx directly instead of super( Class, self).xxx :
Example:
python3 can be written directly as: super().__init__()
python2 must be written as: super(class name, self).__init__( )
Python3.x Example:
class A: def add(self, x): y = x+1 print(y) class B(A): def add(self, x): super().add(x) b = B() b.add(2) # 3
Python2.x Example:
#!/usr/bin/python # -*- coding: UTF-8 -*- class A(object): # Python2.x 记得继承 object def add(self, x): y = x+1 print(y) class B(A): def add(self, x): super(B, self).add(x) b = B() b.add(2) # 3
The bottom layer: write first A parent class A
class A: def __init__(self): print('A')
The second layer: let B, C, D inherit from A
class B(A): def __init__(self): print('B') super().__init__() class C(A): def __init__(self): print('C') super().__init__() class D(A): def __init__(self): print('D') super().__init__()
The third layer: E, F, G inherit
class E(B, C): def __init__(self): print('E') super().__init__() class F(C, D): def __init__(self): print('F') super().__init__() class G(E, F): def __init__(self): print('G') super().__init__()
Look at G Inheritance order
We found that G inherits from E, and F is parallel. During initialization, E will not be initialized first. F.
The following are three different inheritances and calls. Compare their differences and understand the purpose of super().__init__() .
Inherited content | |
---|---|
Inherits all | |
Inherits, but overrides the init method | |
Inherits, but overrides the init method and adds super().__init__() |
The above is the detailed content of How to use the __init__() method of the super() function in Python?. For more information, please follow other related articles on the PHP Chinese website!