In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "how to understand Python dynamic parameters, namespaces, function nesting, global and nonlocal". 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 how to understand Python dynamic parameters, namespaces, function nesting, global and nonlocal.
Dynamic parameters of a function
1.1 * dynamic transfer of args position parameters
Def chi (* food):
Print ("I want to eat", food)
Chi ("rice", "millet rice")
Result: I want to eat ('rice', 'millet') more than # parameters are passed in. The content received is tuple tuple
1.2 * * dynamic parameter transfer of kwargs keyword parameters
Def func (* * kwargs):
Print (kwargs)
Func (axi1, baux2, cymb3)
Func (axi1, baux2)
Results:
{'averse: 1,' baked: 2, 'catered: 3}
{'averse: 1,' baked: 2}
Order: position parameter, * args, default value, * * kwarg
Def func (* args, * * kwargs):
Pass
Location of parameter *, * *: aggregation
Position of argument *, * *: break up
Def fun (* args):
Print (args)
Lst = [1,4,7]
Fun (lst [0], lst [1], lst [2])
Fun (* lst) # can use * to order a list by 2. Namespace
After the python interpreter starts to execute, it will open up a space in memory and record the relationship between the variable name and value whenever it encounters a variable, but when it comes to a function definition, the interpreter just reads the function name into memory, indicating that the function exists. As for the variables and logic inside the function, the interpreter does not care. That is to say, at the beginning, the function is just loaded, that's all, only when the function is called and accessed, the interpreter will open up the internal space of the variable according to the variable declared inside the function. With the completion of the function execution, the space occupied by the internal variables of these functions will be emptied as the function execution completes.
2.1 built-in namespaces-stores the names provided to us by the python interpreter, list, tuple, str, int, etc. These are all built-in namespaces
2.2 Global namespaces-We are directly in the py file, and variables declared outside the function belong to the global namespace
2.3 Local Namespace-variables declared in the function are placed in the local namespace
Loading order: built-in Namespace > > Global Namespace > > Local Namespace (when the function is executed)
Order of values: local namespace > > global namespace > built-in namespace
Scope:
Scope: scope is scope, which is divided into global scope and local scope according to the effective scope.
Global scope: contains built-in and global namespaces. Can be used anywhere in the entire file (follow line by line from top to bottom).
Local scope: can be used within a function.
1. Global scope: built-in + global
two。 Local scope: local (function called)
3. Globals () to view the content in the global
4. Locals () to view the content order in the current scope
A = 10
Def func ():
A = 40
B = 20
Def abc ():
Print ("")
Print (a, b) # Local scope is used here
Print (globals ()) # print content in global scope
Print (locals ()) # print content in local scope
Func ()
Function nesting
Functions can be nested within each other
Def fun1 ():
Print (111)
Def fun2 ():
Print (222)
Fun1 ()
Fun2 ()
Print (111)
# nesting of functions
Def fun2 ():
Print (222)
Def fun3 ():
Print (666)
Print (444)
Fun3 ()
Print (888)
Print (33)
Fun2 ()
Print (555)
Key to global and nonlocal
Global: local access to global content
A = 100
Def func ():
Global a # adds a global to indicate that the variable is no longer created locally. Instead, use the global a directly.
A = 28
Print (a)
Func ()
Print (a)
Nonlocal: the variable closest to him in the local search for the outer function
A = 10
Def func1 ():
A = 20
Def func2 ():
Nonlocal a
A = 30
Print (a)
Func2 ()
Print (a)
Func1 ()
# results:
# added nonlocal
# 30
# 30
# No nonlocal
# 30
# 20
At this point, I believe you have a better understanding of "how to understand Python dynamic parameters, namespaces, function nesting, global, and nonlocal". 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.