How to use the __init__() method of the super() function in Python?

WBOY
Release: 2023-04-27 10:13:13
forward
1866 people have browsed it

What is the use of super().__ init__ ()?

super().__init__() 、 super(B,self).__init__()
Copy after login

1. Understand super() and __ init __() respectively

1.1, super()

How to use the __init__() method of the super() function in Python?

需要注意的是python2、3的super写法稍有不同。
Copy after login
1.2. __init__()

__init__() is a constructor in python, which is "automatically called" when creating an object.

How to use the __init__() method of the super() function in Python?

定义类时可以不写init方法,系统会默认创建,
你也可以写一个,让你的类在创建时完成一些“动作”。
Copy after login
1.3, super(). __ 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.

How to use the __init__() method of the super() function in Python?

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")
Copy after login
1.3.1 , Questions about "coverage"

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()调用。
Copy after login
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()
Copy after login

2. The difference between super() in python2 and 3

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
Copy after login

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
Copy after login

3. About inheritance order

The bottom layer: write first A parent class A

class A:
    def __init__(self):
        print('A')
Copy after login

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__()
Copy after login

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__()
Copy after login

Look at G Inheritance order

How to use the __init__() method of the super() function in Python?

How to use the __init__() method of the super() function in Python?

We found that G inherits from E, and F is parallel. During initialization, E will not be initialized first. F.

4. Compare super (python3) from multiple instances

The following are three different inheritances and calls. Compare their differences and understand the purpose of super().__init__() .

4.1. Instance

How to use the __init__() method of the super() function in Python?

##Subclass nameInherited contentPupleInherits all Puple_InitInherits, but overrides the init methodPuple_SuperInherits, but overrides the init method and adds super().__init__()
4.2. Running results Comparison

How to use the __init__() method of the super() function in Python?

4.3, complete code

How to use the __init__() method of the super() function in Python?

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template