In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the knowledge of "how to understand the _ _ name__ variable of Python". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
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'm going to take you through the use 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 to Python, the _ _ name__ variable (with two underscores before and after it) is quite 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 of the script 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 (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 by 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.
That's all for "how to understand the _ _ name__ variable of Python". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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: 221
*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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.