__init__.py usage explanation

The project structure is as follows:
Insert image description here

test1.py

class A:
    def a1(self):
        print('aaa')

    def a2(self):
        print('a2222')
test2.py

class B:
    def b1(self):
        print("dddd")
test3.py

class C:
    def c1(self):
        print('ccc')

    def c2(self):
        print('a2222')

test4 is the test file

The __init__.py file in the pack1 directory is empty:
Insert image description here
import sub1.test1 and sub1.test2 from test4, you can find:
Insert image description here
uncomment the pack1, and then import the package to find:
Insert image description here

Note: When import pack1 is executed, the __init__.py file under the package will be executed, and you can directly use [package name.method] to directly access the methods in the class, such as pack1.A()

Guess you like

Origin blog.csdn.net/qq_44881930/article/details/129517214