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