Python in __name__ explanation

Source: Internet
Author: User
First, the name variable
1. We first use namedirectly (not defined), if normal use, then the variable is a system built-in variables, as shown in:

As you can tell, name is a built-in variable for the system.
2. When name is used in the currently executing file, its value is main, but when name is in the called module, its value is the path where the called file is located, see:

In summary: Thename variable belongs to the system variable when it is used within the execution file, its value is main, but when used in other called modules, its value is the path to which the called module belongs;

Second, the use of thename variable in the actual role
We often see the use of re-programs

If name = = 'main':
Pass
When we write the program, often is the function module and the business logic module separates, when writes the function function, often has to carry on the debugging function is normal, then we may at this time directly at the function module bottom add if name = = 'main': Pass. This allows you to test function functions directly below.
At the same time, when these functions are called by other modules, the following code block will not be executed at this point, so that if you call other modules, the IF function underneath the called module will not be executed (the code for the entire module will be loaded first when the import module is made. can refer to http://blog.51cto.com/10836356/2095490), which will not affect the normal use of the entire program.
All in all: Easy to test the program.

Python in __name__ explanation

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.