1. Dynamic Parameters
Parameters are passed normally when the number of arguments is equal to the number of formal parameters.
Def func1 (A, B, c): passfunc1 (1, 2, 3)
When the number of arguments is not equal to the number of formal parameters, an error is encountered.
Def func1 (A, B, c): passfunc1 (1, 2, 3, 4, 5, 6, 6)
When the number of arguments passed into the function is indeterminate, or for later expansion, the dynamic parameter *args **kwargs (universal parameter) is used at this time.
Order:
Positional parameters, *args, default parameters, **kwargs
1, *args
*args is to receive all positional parameters.
def func (*args, **kwargs): print (args) print (Kwargs) func (1, 2, 3, 4)
Note: The args in print () cannot be preceded with *.
2, **kwargs
The **kwargs receives all the keyword arguments.
def func (*args, **kwargs): print (args) print (Kwargs) func (x=4, y=5, z=6)
Note: The args in print () cannot be preceded with *.
def func (*args, **kwargs): print (args) print (Kwargs) func (1, 2, 3, x=4, y=5, z=6)
Order
Positional parameters, default parameters
Def Func2 (A, b, sex = ' male '): print (a) print (b) print (Sex) func2 (1, 2, ' female ')
Positional parameters, *args, default parameters
Def Func2 (A, b,*args, sex = ' male '): print (a) print (b) print (args) print (Sex) func2 (1, 2, 5,6,7,8,9, sex= ' Female ')
Positional parameters, *args, default parameters, **kwargs
Def Func2 (A, B, *args, sex= ' man ', Age=20, **kwargs): print (a) print (b) print (args) print (Sex) Print (age) print (Kwargs) Func2 (1, 2, 5, 6, 7, 8, 9, x=6, y=5, Name= ' Alex ')
Adding device:
def sum1 (*args): ret = 0 for i in args: ret + = i return retprint (sum1 (1, 2, 3,))
def func3 (*args, **kwargs): print (args) print (kwargs) L1 = [1, 2, 3]l2 = [One, one, 32]func3 (L1, L2)
def func3 (*args, **kwargs): print (args) print (kwargs) L1 = [1, 2, 3]TU1 = (1, 2, 3) TU2 = (one,) func3 (*L1, *tu 1, *TU2)
def func3 (*args,**kwargs): print (args) func3 (1, 2, 3, 1, 2, 3, 11, 22, 33)
In the definition of a function, * is the meaning of aggregation.
def func3 (*args,**kwargs): print (args) print (kwargs) L1 = [1, 2, 3]dic ={' name ': ' Alex '}dic1 ={' age ': 1000}func3 (*l1, **dic, **dic1)
* The use of Magic:
def func3 (*args,**kwargs): # function definition * is intended to be aggregated. print (args) print (kwargs) L1 = [1, 2, 3]TU1 = (1, 2, 3) TU2 = (one, one, one) func3 (*L1, *TU1, *TU2)
def func3 (*args,**kwargs): print (args) print (Kwargs) func3 (1,2,3,1,2,3,11,22,33)
def func3 (*args,**kwargs): print (args) print (kwargs) L1 = [1, 2, 3]dic ={' name ': ' Alex '}dic1 ={' age ': 1000}func3 (*l1, **dic, **dic1)
2. Namespace, scope, scope chain, load order 1, Space name
2. Scope
3. Scope Chain
4. Loading order
3. Nesting of functions
4, Global, nonlocal 1, Global
2, nonlocal
Python full stack __ Dynamic parameters, namespaces, scopes, scope chains, load order, function nesting, global, nonlocal