In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "what are the knowledge points of python namespaces". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what are the knowledge points of python namespaces"?
Definition of namespaces
The space that the python interpreter opens up in memory when loading py files, which uses dictionaries to store objects and values. The keys of the dictionary hold the variable name, method name, class name, and other environment variable names in the py file, and the corresponding values hold the value of the object (or memory address or None).
Print (globals ()) out: {'_ name__':'_ _ main__','_ _ doc__': None,'_ _ package__': None,'_ _ loader__':,'_ _ spec__': None,'_ _ annotations__': {},'_ _ builtins__':,'_ _ file__': 'd:/Python/ function / case about the function .py' Classification of'_ _ cached__': None} namespaces
Global (Global names) namespace
The namespace created when the python interpreter starts loading the py file, storing objects including global variable names, method names, class names, and other environment variables.
There is only one global namespace.
Local (Local names) namespace
A temporary namespace created by the python interpreter when a method, class, and so on is executed, and the objects stored are variable names, method names, and so on within the method or class.
There can be multiple local namespaces, which are destroyed when the method is executed.
Methods can be defined within methods: outer method-> outer local namespace; inner method-> inner local namespace.
Built-in (Built-in names) namespace
The python interpreter is used to save methods in the internal module builtins.
There is only one built-in namespace.
Loading order of namespaces
Built-in (Built-in) namespace
Global (global) namespace
Local (local) namespace
Value order-> LEGB principle
When the object is called, python looks for it in the following order:
Local (local) namespace
Non-local (Enclosing) namespace
Global (Global) namespace
Built-in (Built-in) namespace
An error will be reported if the object name cannot be found after the above four steps.
Under the same namespace, the name and object are one-to-one. Under different namespaces, there can be three objects in a name: local objects, non-local objects, and global objects. Python will look up the nearest object according to the LEGB principle.
Text = 123def out (): text = 456 print ('out',text) def inner (): text = 789 print (' inner',text) inner () out () print (text) out:out 456inner 789123 about Global and nonlocal
Objects in global (Global) namespaces and non-local (Enclosing) namespaces can be referenced directly in inner-layer methods.
Text1 = 'global namespace variable' def out (): text2 = 'outer namespace variable' def inner (): print (text1) print (text2) inner () out () out: global namespace variable outer namespace variable
An error is reported when the inner method modifies objects in the global (Global) namespace and the non-local (Enclosing) namespace.
Text1 = 'global namespace variable' def out (): text2 = 'outer namespace variable' def inner (): # UnboundLocalError: local variable 'text1' referenced before assignment text1 + =' print on inner layer'# UnboundLocalError: local variable 'text2' referenced before assignment text2 + =' print on inner layer 'print (text1) print (text2) inner () out ()
If a change is really needed, it must be declared before it can be modified.
Text1 = 'global namespace variable' def out (): text2 = 'outer namespace variable' def inner (): global text1 nonlocal text2 text1 + = 'modify at the inner layer' text2 + = 'modify at the inner layer' print (text1) print (text2) inner () out () out: the global namespace variable modifies the outer namespace variable at the inner layer and modifies the outer namespace variable at the inner layer I believe that you have a deeper understanding of "what are the knowledge points of python namespaces?" 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.
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.