A detailed description of the test for name__ values in Python

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.