I. Introduction argument between Form
Parameter: parameter called the formal parameters defined in the definition phase function, referred to as parameter
def func(x, y): # x = 1,y =2
print(x, y)
Argument: the incoming call function called actual parameter value stage, referred to as the argument, equivalent to the value of variable
func(1, 2)
Relations between Form argument:
1, the call phase, the argument (variable value) will be assigned (memory address binding) for parameter (variable name)
2, this binding relationship can only be used in the body of the function
3, the solid relations binding the formal parameters involved when the function call to take effect after the end of the function call unbind
Argument value corresponds, but the value may be in the form
in the form of a:
func(1,2)
In the form of two:
a = 1
b = 2
func(a, b)
In the form of three:
func(int('1'), 2)
func(func1(1,2,),func2(2,3),333)
Second, the parameters of actual and specific use
Positional parameters: sequentially from left to right according to the parameters defined position parameter called
the position parameter: variable name in accordance with the order from left to right directly defined
characteristics; position parameter values must be transferred, at least one more not a does not work
def func(x, y):
print(x, y)
Argument position: in order from the left of a guide value passed
features: one-order parameter and
func(1, 2, )
2.2 Keyword parameters
Keyword argument: the function call phase, in the form of key = value of the passed-in value
def func(x, y):
print(x, y)
func(y=2, x=1)
func(1, 2)
Mix, emphasizing
1, the position of the argument must be placed before the keyword arguments
def func(x, y):
print(x, y)
func(1, y=2)
func(y=2,1) # 语法错误 位置实参必须放在关键字实参前
2, can not be repeated for the same transmission parameter values
def func(x, y):
print(x, y)
func(1,y=2,x=3) # x重复传值
func(1,2,y=3,x=4) # x,y重复传值
2.3 default parameters
The default parameters: function definition stage, a long parameter has been assigned
Features: definition phase has already been assigned, which means no assigned values in the calling stage
def func(x, y=3):
print(x, y)
func(x=1)
func(x=1, y=4444)
def register(name, age,gender='男'):
print(name, age, gender)
register('wndmd', 18, )
register('茄子', 18, )
register('白给', 18, )
register('小茄子', 18, '女')
Location shaped participate default parameter mix, emphasizing:
1, the position parameter must be left in the default parameter
def func(x=1,y): # 报错
pass
SyntaxError: non-default argument follows default argument
2, the default value of the parameter is assigned the function definition stage:
A demonstration:
m = 2
def func(x, y=m): # y==> 2的内存地址
print(x, y)
m = 33333333333
func(1)
Model II:
m = [11111111, ]
def func(x, y=m): # y==> [11111111, ]的内存地址
print(x, y)
m.append(333333)
func(1)
3, although default values may be specified as any data type, variable type but not recommended
Function Ideal state: call the function only has a relationship with the function itself, free from outside influence Code
Parameters (Usage * and **) 2.4 variable length
variable length refers to the function call, the number of values (argument) is not fixed incoming
two argument parameter is used for the assignment, Therefore corresponds, for overflow a corresponding argument must be received parameter
m = [11111111, ]
def func(x, y=m):
print(x, y)
m.append(333333)
func(1)
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)
Variable length parameter 2.4 (* and ** usage)
Variable length refers to the function call, the number of values (argument) is not fixed incoming
two argument parameter is used for the assignment, the corresponding, actual parameters for the overflow must have a corresponding shape reference to receive
2.4.1 position parameter variable length
I: parameter names: for receiving location argument overflow, overflow position of the real participants are saved as a tuple format then assigned parameter name immediately following the
* can be any name the heel, but a convention should be args
def func(x,y,*z): # z =(3,4,5,6)
print(x,y,z)
func(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)
II: may be used in the arguments, the argument with the value of the first argument * broken into position
def func(x,y,z):
print(x,y,z)
func(*[11,22,33]) # func(11,22,33)
func(*[11,22]) # func(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]) # func(1,2,3,4,5,6)
func(*'hello') # func('h','e','l','l','o')
Keyword parameters 2.4.2 variable length
I: parameter names: means for receiving keyword argument spilled, will save the argument into the overflow keyword dictionary format, and then assigned to the parameter name in the immediately following
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 , the first value is broken into 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)
错误
func(**{'x':1,'y':2,}) # func(x=1,y=2)
func(**{'x':1,'a':2,'z':3}) # func(x=1,a=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 before it must 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)
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) # 为wrapper传递的参数是给index用的
Original format --- "Summary -----" show their colors