What is the __name__ variable in Python?

Preface

The text and pictures in this article are from the Internet and are for learning and communication purposes only. They do not have any commercial use. If you have any questions, please contact us for processing.

PS: If you need Python learning materials, you can click on the link below to get it yourself

Python free learning materials and group communication answers Click to join


You should have seen the name  variable in many Python scripts,  right? It often appears in our programs in a way like this:

if __name__ ==  __main__ :
    main()

Today, I will take you to take a detailed look at the usage of this built-in variable and demonstrate how to use it in your Python module.

 What is this  name for?

As a built-in variable of Python, the name  variable (with two underscores before and after) is quite special. It is a necessary attribute for every Python module, but its value depends on how you execute this code.
In many cases, it is impossible to put all your code in the same file, or the functions you write in this file can be used in other places. In order to reuse these codes more efficiently, you need to import code from other files in your Python program.
So,   with the help of the name variable, you can tell whether the code is being run directly or imported into other programs.

 What value may this name variable take?

When you execute a script directly, the  name variable of this script is equal to  main . When this script is imported into other programs, the name  variable is equal to the name of the script itself.
Below, let me cite two chestnuts to illustrate:

 

Case 1-Run the script directly

Suppose we have a nameScript.py, the code is as follows:

def myFunction():
    print( 变量 __name__ 的值是   + __name__)
def main():
    myFunction()
if __name__ ==  __main__ :
    main()

When you execute nameScript.py directly, the process is handled like this:


Before all other code is executed, the __ name __ variable is set to __ main __ . After that, by executing the def statement, the body of the functions main() and myFunction() are loaded.
Then, because the expression following this if statement is true, the function main() is called. The main() function calls myFunction() to print out the value of the variable __main__ .

 

Case 2-Import from other scripts

If you need to reuse this myFunction() function in other scripts, such as importingScript.py, we can import nameScript.py as a module.
Suppose the content of importingScript.py is as follows:

import nameScript as ns
ns.myFunction()

At this time, we have two different scopes: one is importingScript and the other is nameScript. Let me draw a diagram, and you can see the difference from before:


In importingScript.py, the __ name __ variable is set to  __ main __ . When importing nameScript, Python searches for the .py file with the corresponding name in the path pointed to by the local and environment variable PATH. After finding it, it will run the code in the imported file.
But this time, when importing, its own  __ name __ variable is set to nameScript, and the following is the same, the body of the functions main() and myFunction() are loaded. However, this time the result of the expression after the if statement is false, so the main() function is not called.
After importing, go back to importingScript.py. Now the function definition in the nameScript module has been imported into the current scope, so we call the function in the module by ns.myFunction(), this function returns the value of the variable nameScript in the module.
If you try to print the value of the name__ variable in the importingScript, it will also output __main when you execute the importingScript directly. The reason is that this variable is in the scope of importingScript.

 

in conclusion

Today I discussed with  you the characteristics of the  __ name __ variable in the module, and analyzed the effect of different calling methods on its value. Using this feature, you can either import the module in the program to use it, or directly run the module itself as a program.

Guess you like

Origin blog.csdn.net/pythonxuexi123/article/details/112872574