Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the function of the _ _ name__ variable of Python

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "what is the function of the _ _ name__ variable of Python". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is the function of the _ _ name__ variable of Python"?

I know you must have seen the variable _ _ name__ in the Python script more than once. It mostly appears in our programs in this way:

If _ _ name__ = ='_ _ main__':

Main ()

I know you must have seen it, so do you really know _ _ name__?

This Python tutorial will give you a good understanding of this built-in variable and its use, and demonstrate how to use it in the Python module you write.

I wonder what _ _ name__ does with it?

As a built-in variable to Python, the _ _ name__ variable (with two underscores before and after it) is really special. It is an essential attribute for every Python module, but its value depends on how you execute the 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 elsewhere. To reuse this code 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 run directly or imported into another program.

What is the possible value of this _ _ name__ variable?

When you execute a script directly, the _ _ name__ variable equals'_ _ main__',. When the script is imported into another program, the _ _ name__ variable equals the name of the script itself.

Next, let me give you two chestnuts to illustrate.

Scenario 1-run the script directly

Suppose we have a nameScript.py with the following code:

Def myFunction ():

Print (the value of the variable _ _ name__ is'+ _ _ name__)

Def main ():

MyFunction ()

If _ _ name__ = ='_ _ main__':

Main ()

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

The _ _ name__ variable is set to'_ _ main__' before all other code is executed. After that, the ontology of the functions main () and myFunction () is loaded by executing the def statement.

Then, because the expression after the if statement is true true, the function main () is called. The main () function calls myFunction (), which prints 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 in 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 point, we have two different scopes: one for importingScript and one for nameScript. Let me draw a diagram and you can see the difference between this and the past:

In importingScript.py, the _ _ name__ variable is set to'_ _ main__'. When importing nameScript, Python looks for the .py file with the corresponding name in the path pointed to by the local and environment variable PATH, and after finding it, it will run the code in the imported file.

But this time, on import, its own _ _ name__ variable is set to 'nameScript', and then again, the ontology of the functions main () and myFunction () is loaded. This time, however, the expression following the if statement turns out to be a fake false, so the main () function is not called.

After the import is complete, go back to importingScript.py. Now that the function definition in the nameScript module has been imported into the current scope, we call the function in the module through ns.myFunction (), which returns the value 'nameScript' of the variable in the module.

If you try to print the value of the _ _ name__ variable in importingScript, it will also output'_ _ main__' when you execute importingScript directly. The reason is that this variable is in the scope of importingScript.

At this point, I believe you have a deeper understanding of "what is the role of the _ _ name__ variable of Python". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report