"Python Advanced Series" Part 1: Using Python Packages to Organize Code

        Use Python package (package) to organize code
        Recently, when I was looking at the Python introductory skill tree, I saw the Python package organization code and thought it was very interesting, so I wrote a note to summarize it.

        Python organizes code through packages, which are a special kind of modules.

        Python packages come in two forms, Regular packages and namespace packages.

        The so-called Regular packages refer to the directory containing __init__.py. When such a package is imported by other modules, the code in __init__.py under the directory will be executed first. Regular packages can be nested, that is, a subdirectory under a directory can also be a package.

1. Preliminary preparation

Suppose the code in parent/__init__.py is:

print('从前有座山,')

Suppose the code in parent/one/__init__.py is:

print('山上有座庙,')

Suppose the code in parent/one/one/__init__.py is:

print('庙里有个老和尚,')

Suppose the code in parent/two/__init__.py is:

print('老和尚说:')

Suppose the code in parent/three/__init__.py is:

print('从前有座山,')

Create the corresponding py file as shown above. The corresponding effect is shown in the figure below.insert image description here

 The specific file content corresponds to the following:

# |-parent:
# |---print('从前有座山,')
# |---one:
# |-------print('山上有座庙,')
# |-------one:
# |-----------print('庙里有个老和尚,')
# |---two:
# |-------print('老和尚说:')
# |---three:
# |-------print('从前有座山,')

Two, a variety of import calling methods

        If a module has been imported, Python will cache the imported module in the sys.modules dictionary, and it will not re-execute the import action when it is imported again, and it will be taken directly from the cache. Conversely, if we delete the imported module from sys.modules, the import will trigger the module import action again.

        Let me introduce del first : del in python is used to delete the reference of the corresponding variable . Since python is all references, and python has a GC mechanism, the del statement acts on variables, not data objects.

if __name__ == '__main__':
    a = 1  # 对象 1 被 变量a引用,对象1的引用计数器为1
    b = a  # 对象1 被变量b引用,对象1的引用计数器加1
    c = a  # 1对象1 被变量c引用,对象1的引用计数器加1
    del a  # 删除变量a,解除a对1的引用
    del b  # 删除变量b,解除b对1的引用
    print(c)  # 最终变量c仍然引用1

In a word, del deletes variables, not data.

experiment

if __name__ == '__main__':
    while True:
        import parent.one.one
# 从前有座山,
# 山上有座庙,
# 庙里有个老和尚,

The import parent.one.one operation will print the contents of three __init__.py at the same time.

if __name__ == '__main__':
    while True:
        import parent.one.one
        import parent.two

# 从前有座山,
# 山上有座庙,
# 庙里有个老和尚,
# 老和尚说:

        This is because Python will cache the imported modules in the sys.modules dictionary, and will not re-execute the import action when importing again, and take it directly from the cache. So only one printout.

if __name__ == '__main__':
    while True:
        import parent.one.one

        del sys.modules['parent.one.one']

# 从前有座山,
# 山上有座庙,
# 庙里有个老和尚,
# 庙里有个老和尚,
# 庙里有个老和尚,
# ...

        This is because after deleting the imported module of 'parent.one.one' from sys.modules, importing again will trigger the module import action again.

        Therefore, in order to output the corresponding content in a loop, it is necessary to delete the imported modules one by one in each loop, so that the loop can be started!

Task: Using the above knowledge, our goal is to import the module through the import statement, and trigger the execution of the print statement when __init__.py in each package directory is loaded, thereby printing out in a loop:

从前有座山,
山上有座庙,
庙里有个老和尚,
老和尚说:
从前有座山,
山上有座庙,
庙里有个老和尚,
老和尚说:
从前有座山,
...

Once you know the above and experiment, it's easy! first way of writing
 

import sys

if __name__ == '__main__':
    while True:
        import parent.one.one
        import parent.two
        import parent

        del sys.modules['parent.one']
        del sys.modules['parent.one.one']
        del sys.modules['parent.two']
        del sys.modules['parent']

second way of writing
 

import sys

if __name__ == '__main__':
    while True:
        import parent.one.one
        import parent.two
        del sys.modules['parent']
        del sys.modules['parent.one']
        del sys.modules['parent.one.one']
        del sys.modules['parent.two']

third way of writing
 

import sys

if __name__ == '__main__':
    while True:
        import parent
        import parent.one
        import parent.one.one
        import parent.two

        del sys.modules['parent']
        del sys.modules['parent.one']
        del sys.modules['parent.one.one']
        del sys.modules['parent.two']

Reference
https://edu.csdn.net/skill/practice/python-3-6/164?typeId=17350&language=python
https://www.it610.com/article/1521467954061967360.htm

Guess you like

Origin blog.csdn.net/liuqinhou/article/details/129901205