Parameter: defining a function parameter
arguments: the argument when calling the function
between Form argument relations:
- When you call the real parameter binding participants
- Binding relationship can only be used in the function body
- Take effect only when the actual call to participate in the formal parameter binding relationship, and after the end of the call to unbind
Argument passed by value:
# A form: func(1, 2) # In the form of two: a = 1 b = 2 func(a, b) # In the form of three: func(int('1'), 2)
Shaped involved in the use of arguments:
Location parameters:
Location parameter: a function definition, left to right, as defined
Features: must be transferred value, there are several parameter values, it must pass several values
Position argument: value-passing sequentially from left to right order when called
Features: pass the corresponding parameter value in accordance with the order
def func(x,y): print(x,y) func(1,2)
Keyword arguments:
Keyword parameter: in the form of keys and the value of the function call by value
Features: The "variable name" value and may not be passed by value in accordance with the order
def func(x,y): print(x,y) func (y = 2, x = 1)
Mix positional parameters and keyword arguments:
1, the position of the argument must be placed before the keyword arguments
func (1, y = 2)
2, to the same can not pass a plurality of parameter values
func (1, y = 2, x = 3) # Error demo
Default parameters: defining a function has been assigned parameter
Features: When defining been assigned the assignment when you can not call, of course, be reassigned
def func(x,y=3): print(x,y) func (x = 1, y = 44444) #y when the parameter definition has been assigned, but the function is called may be reassigned
def register(name,age,gender='男'): print(name,age,gender) Register ( "three guns', 18) Register ( 'Artillery', 19) Register ( "guns", 19) register ( 'no gun', 19, 'female')
Mixing position parameter using default parameters:
1, the position parameter must be left in the default parameter
def func(y=2,x): pass
2, the value of the default parameter is defined in the function assignment, accurate to say that given the memory address
m=2 def func (x, y = m): # y => 2 memory address print(x,y m=3333333333 func(1)
3, default values can specify any type, but does not recommend the use of variable type, since this would change the function logic of the entire mess.
Function ideal state: call the function only has a relationship with the function itself, not to be influenced by external code.
def func(x,y,z,l=None): if l is None: l=[] l.append(x) l.append(y) l.append(z) print(l) func(1,2,3) func(4,5,6) new_l=[111,222] func(1,2,3,new_l)
Parameters (* and ** usage) variable length
variable length refers to the function call, the number of values (argument) is not fixed incoming
and argument is used as the parameter assignment, so corresponds to, for argument must have overflow parameter corresponding to the received
variable length position parameters
the I: * parameter name: overflow position for receiving arguments, the position of the overflow real participants are saved as tuples * the format then assign closely followed by the parameter name
* heel can be any name, but the convention should be args
def my_sum(*args): res=0 for item in args: res+=item return res res=my_sum(1,2,3,4,) print(res)
II: * can be used in the arguments, the argument with *, the value of a position to break the arguments *
def func(x,y,z): print(x,y,z) # Func (* [11,22,33]) # operation (11,22,33) # Func (* [11,22]) # operation (11.22) l=[11,22,33] func (* l)
III: Types of actual parameters are marked with *
def func(x,y,*args): # args=(3,4,5,6) print(x,y,args) func(1,2,[3,4,5,6]) func (1.2 * [3,4,5,6]) # operation (1,2,3,4,5,6) func (* 'hello') # operation ( 'h', 'e', 'l', 'l', 'o')
Variable-length key parameters
the I: ** parameter names: for receiving spilled Keyword argument, ** spill-over into a dictionary keyword argument save format, and then assigned to the parameter immediately following name
** heel can be any name, but the convention should be kwargs
def func(x,y,**kwargs): print(x,y,kwargs) func(1,y=2,a=1,b=2,c=3)
II: ** may be used in the argument (** heel only dictionary), with the arguments *, ** value of the first argument broken into Categories
def func(x,y,z): print(x,y,z) func (* { 'x': 1, 'and' 2, 'z': 3}) # func ( 'x', 'y', 'z') func (** { 'x': 1 'and' 2, 'z': 3}) # func (x = 1, y = 2, z = 3)
III: Types of actual parameters are with **
def func(x, y, **kwargs): print(x, y, kwargs) func(y=222, x=111, a=333, b=444) func(**{'y': 222, 'x': 111, 'a': 333, 'b': 4444})
Mix * and **: * args must before ** kwargs
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)