[directory]
1, Introduction to formal parameters and actual parameters
II. Specific use of formal parameters and actual parameters
2.1 location parameters
2.2 key parameters
2.3 default parameters
2.4 variable length parameters (* and * * usage)
2.4.1 variable length position parameters
2.4.2 variable length key parameters
2.5 named key parameters
2.6 combined use
1, Introduction to formal parameters and actual parameters
1. Parameter -- the parameter defined in the function definition stage is called formal parameter, which is called parameter for short, equivalent to variable name
def func(x, y): # x=1,y=2 print(x, y)
2. Argument -- the value passed in during the function call phase is called the actual parameter, which is called the argument for short, equivalent to the variable value
func(1,2)
3. The relationship between formal parameter and actual parameter
#1. In the call phase, the actual parameter (variable value) will be bound to the parameter (variable name)
#2. This binding relationship can only be used in the function body
#3. The binding relationship between arguments and parameters takes effect at the time of function call, and the binding relationship will be released at the end of function call
4. The form of argument
#An argument is an incoming value, but the value can be in the following form:
Form 1: parameter directly replaces function parameter func(1,2) Form 2: assign a value to the variable first, and then put the variable into the function parameter a=1 b=2 func(a,b) Form 3: use the function call (the final result is a value) as a parameter func(int('1'),2) func(func1(1,2,),func2(2,3),333)
II. Specific use of formal parameters and actual parameters
2.1 position parameters: the parameters defined from left to right are called position parameters
(1) Position parameter: in the function definition stage, the "variable name" is defined directly from left to right
Features: must be transferred, one more, one less
def func(x,y): print(x,y) func(1,2) # func(1,2,3) #TypeError: func() takes 2 positional arguments but 3 were given # func(1,) #TypeError: func() missing 1 required positional argument: 'y'
(2) Location argument: in the function call phase, the values passed in sequence from left to some
Features: one by one correspondence with parameters in order
def func(x,y): print(x,y) func(1,2) func(2,1) 1 2 2 1
2.2 key parameters
(1) Keyword argument: in the function call stage, the value passed in as key=value
Features: it can pass values to a parameter without referring to the order
def func(x,y): print(x,y) # func(1,2) #Location parameter func(y=2,x=1) #Keyword arguments
(2) Mixed use
Stress:
#1. Position arguments must be placed before keyword arguments
def func(x,y): print(x,y) func(1,y=2) # 1 2 # func(y=2,1) #SyntaxError: positional argument follows keyword argument
#2. Cannot pass values repeatedly for the same parameter
def func(x,y): print(x,y) func(1,y=2) # 1 2 func(1,y=2,x=3) #TypeError: func() got multiple values for argument 'x' func(1,2,x=3,y=4) #TypeError: func() got multiple values for argument 'x'
2.3 default parameters
(1) Default parameter: the parameter that has been assigned in the function definition phase is called the default parameter
Feature: it has been assigned in the definition phase, which means that it is not necessary to assign it in the call phase
def func(x,y=3): print(x,y) func(x=1) # 1 3 func(x=1,y=44444) # 1 44444
def register(name,age,gender ='male '): ාgender ='male', where the default value of gender is assigned to male print(name,age,gender) register('boss', 18) register('second child ', 19) register('senior three ', 19) register('senior four ', 19,' female ') Eldest 18 men The second 19 male Junior 19 male Senior 19 female
(2) Position shape participates in the mixing of default parameters
Emphasize:
#1. The position parameter must be on the left side of the default parameter
def func(y=2,x): # y=2 Is the default parameter, x Is a positional parameter; pass
# SyntaxError: non-default argument follows default argument # Namely Default parameterShould be placed at the end of a function argument
#2. The value of the default parameter is assigned in the function definition stage. To be exact, it is the memory address of the value
Model 1:
m=2 def func(x,y=m): # y=>2 Memory address of print(x,y) m=3333333333333333333 # m It used to point to the memory address of 2. When a new value is assigned, it points to the memory address of the new value. and y The memory address pointed to has not changed func(1)
Output results:
1 2
Model 2:
m = [111111, ] def func(x, y=m): # y=>list [111111, ] The memory address of a container print(x, y) m.append(3333333) func(1)
Output results:
1 [111111, 3333333]
(3) Define function defaults -- although defaults can be specified as any data type, variable types are not recommended
#The most ideal state of a function: the call of a function is only related to the function itself, not influenced by external code
That is to define the default value of a function. The best result is to predict the result it can get. That is to say, the output of the default value must be reliable
The following chestnuts perfectly show that the default result of function execution is affected by the function body——
m = [111111, ] def func(x, y=m): print(x, y) m.append(3333333) func(1) //Output results: 1 [111111, 3333333]
m = [111111, ] def func(x, y=m): print(x, y) m.append(444444) func(2) //Output results: 2 [111111, 444444]
Chestnut 2: Although the default value can be specified as any data type, variable type is not recommended
def func(x,y,z,l=None): # The default value definition of a function can also be written like this func(x,y,z,l=[]):,however l=[],List is a variable type and is not recommended if l is None: l=[] # You can define the variable type default value again in the function body l.append(x) l.append(y) l.append(z) print(l) func(1,2,3) #[1, 2, 3] func(4,5,6) #[4, 5, 6] new_l=[111,222] func(1,2,3,new_l) #[111, 222, 1, 2, 3]
2.4 variable length parameters (* and * * usage)
Variable length refers to the variable number of values (arguments) passed in when a function is called
The argument is used to assign a value to a parameter, so the overflow argument must have a corresponding parameter to receive
2.4.1 position parameters of variable length
1. * parameter name: used to receive the overflow location parameter. The overflow location parameter will be * saved as a meta group format and assigned the parameter name immediately following it
* it can be followed by any name, but the Convention is args
def func(x,y,*z): # z =(3,4,5,6) print(x,y,z) func(1,2,3,4,5,6) # 1 2 (3, 4, 5, 6)
def my_sum(*args): res=0 for item in args: res+=item return res res=my_sum(1,2,3,4,) print(res) # 10
2. * can be used in arguments with * in them. Break the value after * into position arguments first
def func(x,y,z): print(x,y,z) func(*[11,22,33]) # func(11,22,33) # 11 22 33 func(*[11,22]) # func(11,22) # TypeError: func() missing 1 required positional argument: 'z' l=[11,22,33] func(*l) # 11 22 33
3. Both formal parameter and actual parameter have*
def func(x,y,*args): # args=(3,4,5,6) print(x,y,args) func(1,2,[3,4,5,6]) # Output 1 2 ([3, 4, 5, 6],) func(1,2,*[3,4,5,6]) # func(1,2,3,4,5,6),Output 1 2 (3, 4, 5, 6) func(*'hello') # func('h','e','l','l','o') Output result h e ('l', 'l', 'o')
2.4.2 variable length key parameters
1. * * parameter name: used to receive the overflow keyword argument. The * * will save the overflow keyword argument in dictionary format and assign it to the parameter name immediately following it
The * * can be followed by any name, but it's a convention that it should be kwargs
def func(x,y,**kwargs): print(x,y,kwargs) func(1,y=2,a=1,b=2,c=3)
2. The * * can be used in the actual parameter (* * can only be followed by a dictionary), with * * in the actual parameter. First, break the value after * * into the keyword argument
def func(x,y,z): print(x,y,z) func(*{'x':1,'y':2,'z':3}) # func('x','y','z') func(**{'x':1,'y':2,'z':3}) # func(x=1,y=2,z=3) # error func(**{'x':1,'y':2,}) # func(x=1,y=2) # TypeError: func() missing 1 required positional argument: 'z' func(**{'x':1,'a':2,'z':3}) # func(x=1,a=2,z=3) # TypeError: func() got an unexpected keyword argument 'a'
3. Both formal parameter and actual parameter have**
def func(x,y,**kwargs): print(x,y,kwargs) func(y=222,x=111,a=333,b=444) #Output 111 222 {'a': 333, 'b': 444} func(**{'y':222,'x':111,'a':333,'b':4444}) #Output 111 222 {'a': 333, 'b': 4444}
2.4.3 mix * and * *: * args must be before * * kwargs
def func(x,*args,**kwargs): print(args) print(kwargs) func(1,2,3,4,5,6,7,8,x=1,y=2,z=3) # TypeError: func() got multiple values for argument 'x'
def index(x,y,z): print('index=>>> ',x,y,z) def wrapper(*args,**kwargs): #args=(1,) kwargs={'z':3,'y':2} index(*args,**kwargs) index(*(1,),**{'z':3,'y':2}) index(1,z=3,y=2) wrapper(1,z=3,y=2) # by wrapper The parameter passed is to index Used //Original format > summary > return to original format