In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces the Python dynamic parameters / namespace / function nesting / global and nonlocal example analysis, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.
1. 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") 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, cyste3) func (axi1, baux2)
Results:
{'averse: 1,' baked: 2, 'cased: 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) # you can use * to break up a list in order s = "short leg Luban No.7" fun (* s) # string can also be broken, (iterated object)
two。 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 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 contents of the current scope
A = 10def func (): a = 40 b = 20def abc (): print ("") print (a, b) # here use local scope print (globals ()) # print content in global scope print (locals ()) # print content in local scope func ()
3. Function nesting
Functions can be nested within each other
Def fun1 (): print def fun2 (): print fun1 () fun2 () print (111) # function nested def fun2 (): print def fun3 (): print (666) print (444) fun3 () print (888) print (33) fun2 () print
4. Global and nonlocal key
Global: local access to global content
A = 100def func (): global a # adds a global to indicate that the variable is no longer created locally. Instead, use the global an a = 28 print (a) func () print (a) directly.
Nonlocal: the variable closest to him in the local search for the outer function
A = 10def func1 (): a = 20 def func2 (): nonlocal an a = 30 print (a) func2 () print (a) func1 () # result: # Thank you for reading this article carefully without adding nonlocal# 3pm. I hope the article "dynamic parameters / Namespace / function nesting / example Analysis of global and nonlocal" shared by the editor will be helpful to you, and I hope you will support it. Pay attention to the industry information channel, more related knowledge is waiting for you 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.