Dynamic parameters: One, *args:args is Ganso, it contains all the positional parameters.
1, the first form parameter contains only *args.
def func (*args): Print(*args,type (args)) func (1,2,3,4,5)
only *args are included in formal parameters
2, there are both positional parameters and *args in the second form parameter.
def func (x,y,z,*args): Print(x) print(y) Print(z) print(args) return x,y,z,argsset =func (1,2,3,4,5,6)print(set)
There are both positional parameters and *args .
3, the third parameter has both positional parameters, *args and default parameters.
def func (x,y,*args,sex=' man '): Print(x) Print (y) Print (Sex) Print (args) return x,y,sex,argsset=func (1,2,3,4,5,6)print(set)
also contains positional parameters, default parameters, *args
**kwargs It contains all the keyword parameters:
1, the first form parameter contains only *kwargs.
def func (* *Kwargs) :print(Kwargs,type (Kwargs)) func (a=1,b=2,c=3,)
contains only **args
2, there are both positional parameters and *kwargs in the second form parameter.
def func (x,y,**Kwargs): Print(x) print(y) Print(kwargs) Set=func (1,2,a=1,b=2,c=3,)print(set)
positional parameters and **kwargs
3, the third parameter has both positional parameters, *args, and the default parameters are **kwargs.
def func (x,y,*args,sex=' man ', * *Kwargs) :Print(x) Print (y) Print (Sex) Print (args) return x,y,sex,args,kwargsset=func (1,2,3,4,5,a=3,b=7)print(set)
positional parameters, *args, default parameters, **kwargs
From the above function can be drawn:
In the form parameter sort is: position parameter --*args---**kwargs
Magic Operations: (frees the elements in the list, not in the form of lists and dictionaries.) Also known as: beaten)
def func1 (*args): return argsfunc1 (*[1,2,3,4,5])Print (Func1 (*[1,2,3,4,5]))
Magic Operations, List
defFUNC2 (* *Kwargs):Print(Kwargs) dic={'K4': 1,'K5': 2,'K6': 2}dic1={'K1': 1,'K2': 2,'K3': 2}ss=FUNC2 (**dic,**Dic1)Print(ss)Magic Operations, dictionariesThe step of the function:
namespaces : Divided into global namespaces, local namespaces, built-in namespaces.
At the beginning of the code, the space created to store the "variable name-to-value relationship" is called the global namespace .
The temporary space created in the function's run is called the local namespace .
The space that holds the name provided by the Python interpreter is called the built-in namespace .
Load Order : built-in namespaces (pre-Program load)-Global namespaces (program run: Load from top to bottom) local namespaces (program run: Load only when called)
Order of Values :
Built-in namespaces, local namespaces, and global namespaces, local call spaces
Global invocation: Built-in namespaces, global namespaces
To sum up, in the search for variables, from the small range, one layer to a wide range to find.
Scope:
Scope is the scope of action, according to the effective scope can be divided into global scope and local scope.
Global scope: Contains built-in namespaces, global namespaces , can be referenced anywhere throughout the file, are globally valid
Local scope: Local namespace, only valid locally
Global keyword, nonlocal keyword:
The role of global:
1: Declare a global variable
2: Global (limited to strings, numbers) is required when local scopes want to make changes to the globals of global scope
def func (): Global a = 3func ()print= 1def search (): Global count = 2search ()print(count)
Global:
NOTE: PS: For variable data types (list,dict,set) can be referenced directly without the global
Li = [A.]dic= {'a':'b'}defChange (): Li.append ('a') dic['Q'] ='g' Print(DIC)Print(LI) change ()Print(LI)Print(DIC)for variable data types, you can use no Golobal
The role of nonlocal:
1, global variables cannot be modified.
2, in the local scope, the variables of the parent scope (or the outer scope non-global scope) are referenced and modified, and which layer of the reference, from that layer and the following this variable all occurs
def add_b (): = def do_global ():= ten print(b) def dd_ Nonlocal (): nonlocal b = b + print(b) dd_nonlocal () Print(b) Do_global () print(b) add_b ()
nonlocalNesting and scoping of functions:
Advanced function of Python (2-1)