Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What is the use of name = & # 039 in the py file in Python

Shulou Source: shulou.com Published: 2022-06-01 11:43:13 10月04日 Update

This article mainly explains "what is the use of name == 'main' in py file in Python". The explanation in this article is simple and clear, easy to learn and understand. Please follow the idea of Xiaobian and go deep into it slowly to study and learn "what is the use of name == 'main' in py file in Python"!

What does name == 'main' in py file do?

When looking at Python code, we often see if __name__= "__main__": in the code, what is it? Is it okay if I don't write? What good would it do?

By default, Python code is executed by the interpreter line by line, from top to bottom. Of course, defining a function, without calling, the internal code is not executed. After that, we execute any Python file and execute every function you write from top to bottom, namely print, a(), x.y(), etc.

if __name__= "__main__": This code, the most important thing is this place: __name__exactly equal to what?

Double-underlined variables are generally Python built-in variables and global variables, which Python will handle internally. Here we use two equal to judge whether it is equal to the double underscore main, in fact, we can guess that this name is sometimes equal to sometimes not equal to the main (DE8UG Note: Double underscore is not easy to write, I omit these words).

When is it equal to? Python specifies when the current Python script is invoked. For example, there is a.py, when called with python a.py, name will be assigned to main, at this time if the judgment is true, and the code after this will be executed. Other modules that are not directly called are equal to the name of the module itself, which is actually the name of the py file.

I suggest that you write this paragraph in every Python file. Write the code that needs to be tested in the current py file, as well as the code that we want to execute directly, to the end of this judgment. This way, you can test the code in this file directly, and when you call this file elsewhere, you won't mistakenly execute unnecessary code.

For example:

# a.py

print('a.py')

print ('a module name:',__name__)

# b.py

print('b.py')

print(__name__)

print ('import a')

import a

print ('import a end')

if __name__ == "__main__":

print ('Run this python file to display')

We execute two files separately to see the effect:

✗ python a.py

a.py

a Module Name: __main__

✗ python b.py

b.py

__main__

Import a

a.py

a Module name: a

End of import a

Run this Python file and it will show

Small homework: add the judgment of that sentence if to a, write some code, and then check the execution results of the two files.

Thank you for reading, the above is the content of "what is the use of name == 'main' in py file in Python". After studying this article, I believe that everyone has a deeper understanding of what is the use of name == 'main' in py file in Python. The specific use situation still needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

Tags: File code time module underscore two variable name place that is learn one line content function name case sometimes test run no Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Shulou Information Shulou Technology Apple NVidia