I. Dynamic parameter transfer
1. Positional parameters Dynamic parameter transfer
def func (*args): * Indicates dynamic parameter transfer
* Here is the acceptance of the dynamic parameters of the positional parameter, the received tuple.
def fun (*args): print (args) fun (' aaa ', 3,5,6, ' CCC ') # results (' AAA ', 3, 5, 6, ' CCC ')
2. Dynamic parameter of keyword
def func (**kwargs):
function body (code block)
Func (n=7,m= "AA"):
* * Represents a dynamic parameter that receives a keyword argument, and a dictionary is received.
def fun (**kwargs): print (Kwargs) fun (name= "Tom", age= "male", hobby= "female") # result {' name ': ' Tom ', ' age ': ' Male ', ' hobby ': ' Female '}
Order: Position parameter = = *arg (arguments) (parameter dynamic parameter) = = Default Value = = **kwargs (keyword argument)
The above parameters can be used in a casual combination
3. Comments on functions
def fun ():
"""
Comment that explains what this function is used for
”“”
Print (fun.__doc__) #document文档
def fun (A, A, b): "" " Note that this function is used for what :p Aram A: :p Aram B: : Return:" "" returnfun (5,6) print (fun.__doc__) #打印出函数的注释
4. Receive All Parameters
def fun (*args,**kwargs): #无敌传参
Pass
*,** the position of the actual parameter is scattered
The *,** parameter position is an aggregation
def fun (*args,**kwargs): print (Args,kwargs) li = [' R ', ' N ', ' G ', ' w ', ' I ', ' n ']dic = {' name ': ' Uzi ', ' age ': ' Male ', ' hobby ': ' ADC '}fun (*li,**dic) # results (' R ', ' N ', ' G ', ' w ', ' I ', ' n ') {' name ': ' Uzi ', ' age ': ' Male ', ' hobby ': ' ADC '}
Second, namespace and scope
1. Global scope:
Global namespaces, built-in namespaces
2. Local scope:
Local namespaces
Globals () to view content in the global scope
Locals () view content in the current scope
A = 5 # global variable def fun (): a = ten #局部变量 print (Locals ()) Print (Globals ()) Fun ()
Nesting of functions
Functions can be nested with each other
A = 1def fun (): a = 5 def inner (): a = ten print (a) inner () print (a) print (a) fun ()
Global
1. You can introduce content from the global to the inside of the function
2. Create a variable at the global
(References are referenced when you want to change external values inside the function, and the global variable itself is unsafe and cannot be modified at will)
A = 1def fun (): Global a A + = 5 print (a) # 6print (a) # 1fun () print (a) # 6
Nonlocal
Look for the variable closest to him in the outer function.
A = 1def fun (): a = 5 def inner (): nonlocal a A + = 5 print (a) # print (a) # 5
inner () print (a) # 10print (a) # 1fun () print (a) # 1
Python dynamic parameters, namespaces and scopes, nesting of functions, global and nonlocal