In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains how to use the python built-in functions locals () and globals (). Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to use the python built-in functions locals () and globals ().
In understanding these two functions, let's first understand the concept of namespaces in Python. Python uses something called a namespace to record the trajectory of a variable. A namespace is just a dictionary, its keys are variable names, and the values of the dictionary are the values of those variables.
In fact, namespaces can be accessed like Python dictionaries.
Each function has its own namespace, called the local namespace, which records the variables of the function, including the parameters of the function and the locally defined variables. Each module has its own namespace, called the global namespace, which records the module's variables, including functions, classes, other imported modules, module-level variables and constants. Then there is the built-in namespace, which can be accessed by any module, and it stores built-in functions and exceptions.
When a line of code wants to use the value of variable x, Python looks for variables in all available namespaces, in the following order:
Local namespace-specifically refers to the method of the current function or class. Python will use this variable if the function defines a local variable XJI, and then stop searching. Global namespace-specifically refers to the current module. If the module defines a variable, function, or class named x, Python will use that variable and stop searching. Built-in namespace-global for each module. As a last resort, Python will assume that x is a built-in function or variable.
If Python cannot find x in these namespaces, it will abandon the search and throw an exception for NameError, passing the message There is no variable named'x'.
Local variable function locals example (locals returns a dictionary of name / value pairs):
Example
Def foo (arg, a): X = 1 y = 'xxxxxx' for i in range (10): J = 1 k = I print (locals ()) # the printed result of the calling function foo (1) 2) # {' KTH: 9, 'jacks: 1,' arg':: 9,'y: 'xxxxxx',' x: 1,'a': 2, 'arg': 1}
The difference between from module import and import module. With import module, the module itself is imported, but it maintains its own namespace, which is why you need to use the module name to access its functions or properties (module.function). But with from module import, you actually import specified functions and properties from another module into your own namespace, which is why you can access them directly without referencing the module from which they come from.
Locals is read-only, globals is not.
Locals cannot be modified, but globals can be modified because:
Locals () does not actually return a local namespace, it returns a copy. So modify it, modify the copy, and have no effect on the value of the variable in the actual local namespace. Globals () returns the actual global namespace, not a copy, which is exactly the opposite of locals's behavior.
So any changes to the dictionary returned by globals will directly affect the value of the global variable.
Example
#! / usr/bin/env python z = 7 # defines the global variable def foo (arg): X = 1 print (locals ()) print ('xcoded journal x) locals () [' x'] = 2 # modifies the copy of the local namespace, while the value of the variable in the actual local namespace has no effect. Print (locals ()) print ("x =", x) foo (3) print (globals ()) print ('zigzag) globals () ["z"] = 8 # globals () returns the actual global namespace, modify the value of the variable z print (globals ()) print ("z =", z)
The output is as follows:
{'name__':: 1,' arg': 3} x = 1 {'xcow: 1,' arg': 3} x = 1 {'_ name__':'_ main__','_ doc__': None,'_ package__': None,'_ loader__':,'_ spec__': None,'_ annotations__': {},'_ _ builtins__': '_ _ file__':' test.py','_ _ cached__': None, 'annotations__':: 7,' foo':} z = 7 {'_ name__':'_ _ main__','_ _ doc__': None,'_ _ package__': None,'_ _ loader__':,'_ _ spec__': None,'_ _ annotations__': {},'_ builtins__': '_ _ file__':' test.py','_ _ cached__': None, 'zonal: 8,' foo':} z = 8 so far I believe you have a deeper understanding of "how to use the python built-in functions locals () and globals ()", so 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.