First, dynamic parameters
1, when we need a function to receive multiple positional parameters, but we are not sure we pass the number of these parameters, there is an indefinite length of the parameters, expressed in *args.
1 #If there are no indeterminate parameters, how many positional arguments we give must be passed within the argument2 #If we pass one less parameter, the program will get an error .3 defEat (a,b,c):4 Print(a,b,c)5Eat"Susan","Junk_food","Coco")6 #Eat ("Rice", "Junk_food") # typeerror:eat () Missing 1 required positional argument: ' C '7 8 #so we define an indefinite length parameter *args He is dynamic, you can change the number of parameters you enter9 #Note: Args is a parameter that cannot be printed with *, and it accepts a tuple. Ten defEat (*args): One Print(args) AEat"Susan","Junk_food")#(' Rice ', ' junk_food ')
#when we have positional parameters combined with dynamic parameters, the following#The number of positional parameters must be satisfied when the argument is assigned, not less than the positional parameter given in the formal parameterdefEat (a,b,c,*args):Print(A,b,c,args) Eat ("Susan","Junk_food","Spicy Soup","Rice Bowls","Beer")#Rice junk_food mala (' rice bowls ', ' beer ')Eat"Susan","Junk_food","Spicy Soup")#Rice junk_food mala (' rice bowls ', ' beer ')
2, when we pass the parameter contains the default parameters, and the number of parameters is not sure how to do it?
1 #The parameter contains positional parameters, default parameters, variable length parameters, the default parameters must be placed on the last side, otherwise the default parameters will be overwritten2 #When you assign a value in an argument, you must also place the parameter at the end of the argument if you want to change the default value of the default parameter. 3 defFunc (A, B, C, *args, d=110):4 Print(A, B, c,d, args)5Func (1, 4, 7, 3, 6, 9, 1231,12,3,3,3,54,56,67,5,44,)6Func (1,2,3,4,4,5,6,d = 220)7 #func (1,2,3,4,d = 220,4,5,6) # syntaxerror:positional argument follows keyword argument
3. What do we do when we pass parameters with key words?
1 # when the arguments passed by the argument contain more than one keyword argument, the **kwargs is substituted at the formal parameter, and the received dictionary 2 def Eat (**kwargs): # * * keyword parameter 3 print(Kwargs) 4 Eat (good_food=' rice bowls ', junk_food=" mala " , drink=" pulsation ")
4. What do we do when the above parameters are mixed together?
1 # conditions for mixed use of multiple parameters: Positional parameter *args default parameter **kwargs2def func (A, B, C, *args, d = A, * *Kwargs ):3 print(A, B, C, args, D, Kwargs)4 func (1,4,7,3,5,8,dd=5,f=7, Abc=9)5 func (1,4,7,3,5,8,d=220,f=7,abc=9)
We construct the relevant statements directly in the case of mixing the various parameters listed above, it is important to remember the above order, positional parameters, *args, default parameters, **kwargs.
5, the Invincible pass the argument, this is more awesome.
1 # The parameters of this function. You can receive all the parameters, the invincible parameter 2 def func (*args, * *Kwargs):3 print(args, Kwargs)4 func ( 1, 4, a=8, B = 9, c= 10)
6. Scattering and aggregation
1 #parameters in arguments are iterative objects that can be used to break and aggregate functions2 defEat (*fruit):#Aggregation Narimoto Group3 Print(Fruit)4LST = ["Banana","Eggplant","Cucumber","Pepper","White Pear"]5 #Eat (lst[0], lst[1], lst[2], lst[3], lst[4])6Eat (*LST)#The * Here means breaking the LST.7 defEat (**kwargs):#aggregate all the keyword parameters into a dictionary8 Print(Kwargs)9DIC = {"a": 1,"b": 2,"C": 3}Ten #Eat (a = dic[' a '], B = dic[' B '), c=dic[' C ']) OneEat (**dic)#Break the dictionary and pass the argument in turn A defRead (*args): - Print(args) -Read (*"Oh, hello.")
Second, the problem of scope
Scope in the function can be understood by the block, we run the program, the first load of the built-in namespace, then the global namespace, then the local namespace, the function is a local namespace unique features, we in the function of the variables we define ourselves can not affect the results of global variables, But we can call the variables in the global space inside the function, and the variables in the memory load characteristics are loaded in a certain way, can be seen by the following example.
1A = 10#Global2 defFunc ():#Global3A = 40#Local4b = 20#Local5 defABC ():#Local6D = 30#is local7 Print("haha")8 Print(A, B)#What is the local area of the?9 Print(Locals ())Ten ABC () One #Print (Globals ()) # Prints the contents of the global scope A #Print (Locals ()) # Prints the contents of the local scope, locals () prints the contents of the current function and the local function - func () - Print(Globals ())#print content from the global scope
Iii. Nesting of functions
Function nesting refers to the function can be nested other functions, to solve the problem according to my understanding the best way is to follow the order, existing a normal heart, according to the program from top to bottom execution, meet the function to execute the relevant function part of the content, after the completion of this function, continue just the position of the function call down execution, In this way the complete run down the program will be found in the function of the nested relationship, but I do not recommend to remember any nested relationships, I only care about the results of function execution, can be in order.
1 deffun1 ():2 Print(111)3 deffun2 ():4 Print(222)5 fun1 ()6 fun2 ()7 Print(333)8 9 deffun2 ():Ten Print(222) One deffun3 (): A Print(666) - Print(444) - fun3 () the Print(888) - Print(33) - fun2 () - Print(555) + - deffunc (): + Print("ha ha haha") AA = Func#The function can be assigned a value. The equivalent of. A and Func are the same thing. at func () -A ()
Iv. Global and nonlocal
Global: Looking for content in a globally scoped scope
Nonlocal: Look for content in the upper-level scope, but not globally.
1A = 102 deffunc ():3 GlobalA#The A used here is global.4A = 205 Print(a)6 func ()7 Print(a)# -8 9A = 10Ten deffunc1 (): OneA = 20 A Print(a) - deffun2 (): -Nonlocal A#upper level, and cannot be a global theA = 30 - Print(a) - fun2 () - Print(a) + func1 () - Print(a)
Python-fullstack-s13-day10-python Foundation