1. __name__
Called module. __name is the module name of the called module, and if the function is executed directly, __name__ = the ' __main__ '.
The most widely used statements are: if __name__ = = ' main ':
The purpose of this statement is to enable the Py file to be self-executing and callable.
eg.py
def test ():
Print (' This is a test ')
if __name__ = = ' __main__ ':
Test ()
In this code, you can make the other py file call eg.py, because at this time __name__ is directly executed file name, if not execute, but also can call the test () method, at the same time, when eg.py directly executes, the __name__ is ' __main__ ', The If execution can be seen as the main function in Java;
2. __init__
__init__ usually appear in the PY in two ways: file, function. __init__ appears as a standalone py file in a Python file whose primary purpose is to separate the py file in the folder into a single package, which is contained in a separate PY package. In general, the __init__ file is empty, and next __init__ creates a new class as a function, unlike the __new__ operation, where the __NEW__ function returns an instance of that class. Details can be viewed: https://www.cnblogs.com/Lands-ljk/p/5880483.html
Here's a problem with Python cross-folder references in the VS code environment: The approximate structure is as follows:
Project
--app.py
--folder
Mod1
Mod2
In this case, the Mod1 module in the sibling folder is referenced by the app.py file under Project, and Mod1 has a reference to the MOD2, which requires the creation of an empty filein the folder ___init__.py, and the corresponding import statement to be modified. The code is as follows:
app.py:
From folder. MOD1 Import Mod1_service
Mod1_service ()
MOD1:
From folder. MOD2 Import Mod2_service
Def mod1_service ():
Mod2_service ()
MOD2:
Def mod2_service ():
Print (' MoD import success ')
If the folder is not in the Mod1 . Write all, you will find that mod2, seek reasons: through the import sys print (sys.path) to see when running app.py file to/project as a reference to the directory, will not enter the folder, this path needs to be labeled.
Python __ Function Cross-folder reference