Function parameters python learning

1, Introduction argument between Form

Parameter: the parameters defined in stage called the formal parameters defined function, referred to as parameter, corresponding to the variable name

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 bound to the parameter (variable name)
2, this binding relationship can only be used in the body of the function
3, actual participation in the binding relationship parameter become effective when the function call after the end of the function call unbind

Value argument is passed, but the value may be in the form

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)    #输出1,2
func(func(1,2,),func(2,3))  #先运行参数的内容,因此先输出1,2和2,3,然后再运行外层函数输出None,None

2, the particular parameter of actual and using

2.1 Parameters positions: a position called the parameter according to the parameter defined sequentially from left to right

Location parameter: function definition stage, left to right, the direct definition of "variable name"

Features: must be transferred value, not one more nor one less

def func(x,y):
    print(x,y)
func(1,2,3)
func(1,)

Location argument: the call phase function, in accordance with the order and some from the left to the incoming value

Features: the order of one-parameter

func(1,2)
func(2,1)

2.2 Keyword parameters

Keyword argument: the function call phase, in the form of key = value of the passed-in value

Features: by name to pass a parameter value, the reference sequence can be completely

def func(x,y):
    print(x,y)

func(y=2,x=1)
func(1,2)

Mix, emphasizing:

1) position argument must be placed before the actual keyword parameters

func(1,y=2)
func(y=2,1)

2) can not pass can be repeated for the same value of the parameter

func(1,y=2,x=3)
func(1,2,x=3,y=4)

2.3 default parameters

Default parameter: function definition phase, has been assigned a parameter, called the default parameters

Features: During the definition phase has already been assigned, meaning that the call can not assign a value to stage

def func(x,y=3):
    print(x,y)

# func(x=1)

func(x=1,y=44444)

def register(name,age,gender='男'):
    print(name,age,gender)

register('三炮',18)
register('二炮',19)
register('大炮',19)
register('没炮',19,'女')

Location shaped participate default parameter mix, emphasizing:

1) the position parameter must be left in the default parameter

def func(y=2,x):
    pass

2) The default value of the parameter is assigned in the function definition phase, it is given precisely the value of the memory address
Model 1:

m=2
def func(x,y=m): # y=>2的内存地址
    print(x,y
m=3333333333333333333
func(1)

Model 2:

m = [111111, ]

def func(x, y=m): # y=>[111111, ]的内存地址
    print(x, y)

m.append(3333333)
func(1)

3) Although the default value 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, is not affected by outside code

m = [111111, ]

def func(x, y=m):
    print(x, y)
    
m.append(3333333)
m.append(444444)
m.append(5555)

func(1)
func(2)
func(3)

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
and argument is used as the parameter assignment, the corresponding, actual parameters for the overflow must have a corresponding shape reference to receive

2.4.1 position parameter variable length

1) parameter names: for receiving location argument overflow, overflow position of the real participants are saved as a tuple format then assigned immediately following parameter name
* heel may be any name, 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)

2) 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)

3) are formed with participation of arguments *

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

1) parameter names: means for receiving keyword argument overflow, the overflow will be saved as a keyword dictionary format argument, and then assigned to the parameter name in the immediately following

def func(x,y,**kwargs):
    print(x,y,kwargs)

func(1,y=2,a=1,b=2,c=3)

2) may be used in the argument (heel only dictionary), with the argument value has broken into the key 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)
func(**{'x':1,'a':2,'z':3}) # func(x=1,a=2,z=3)

3) are formed with participation argument **

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

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

Guess you like

Origin www.cnblogs.com/leilijian/p/12520091.html