This article focuses on testing details about name__ values in Python
The code used in the test is as follows:
#test_name0. Pydef Test (): return nameprint nameprint Test () Import test_name1test_name1.test ()
#test_name1. Pydef Test (): print name print name
Execute the commands and results in the Python top-level interpreter as follows:
In [1]: Type (name) out[1]: Strin [2]: Print nameout[2]: Mainin [3]: Import test_name0test_name0test_name0test_name1test_ Name1
Executed in cmd python test_name0.py , the result is as follows:
Mainmaintest_name1test_name1
From this we can see:
(1) in the Python top-level interpreter or directly running scriptname=='main'
(2) In the module that is calledname==module name
(3) An unexpected situation is found, that is, after running the test_name0.py script, import test_name0 or import test_name0 after running test_name0.py the script, the result is
Test_name0test_name0test_name1
Or
Mainmaintest_name1
Instead of
Test_name0test_name0test_name1test_name1
Or
Mainmaintest_name1test_name1
Ask questions on the segmentfault after you have sorted out the key issues, and understand why this problem occurs.
That is: The Python module has a cache, import once and then import, the module top-level scope of the code will not be executed.
(4) The test function in the TEST_NAME0 and test_name1 two modules has the same name, and there is no problem, because the module name can distinguish between the two test functions, but avoid the duplicate names of the modules.