Parameter Description Python function

First, the argument between Form

Parameter : Parameter i.e., by defining a function, the statement in parentheses. It is essentially a parameter variable name, for receiving the transmitted external value.
Arguments : i.e., when calling the function, the incoming values in parentheses, the value may be constant, variable, or a combination of three expression.

def func(x,y)  #x,y是形参
    pass
func(10,11)  #10,11是实参

Note: When you call the function, the argument value (value of the variable) will be assigned to the parameter (variable names), the binding relationship between the two only take effect when the function is called immediately after the end of the contact bind function call

1.1 form, parameters

1, positional parameters : parameters from left to right in the order defined by the position

def foo(a,b)
    print(a,b)
foo(1,2)

注意:
1、位置形参的特性是在调用函数时必须为其传值,而且多一个少一个都不行
2、位置实参与形参一一对应

2, keyword parameters : when calling the function, defined in the form of key = value of the argument , referred to as keyword parameters

def foo(x,y,z):
    print(x,y,z)1
foo(x=1,y=2,z=3)
注意:
1、相当于指名道姓地为形参传值,意味着即便是不按照顺序定义,仍然能为指定参数传值
2、在调用函数时,位置实参与关键字实参可以混合使用,但关键字实参必须要放在位置实参后,不能为同一个形参重复传值

3, default parameters : the definition phase, is already a reference shape assignment, this parameter is called the default parameters

注意:
1、定义阶段已经有值,意味着调用阶段可以不传值,但如果在实参的时候传入新参数,就会使用新参数
def register(name,age,sex='male'):
    print(name,age,sex)
register('egon',18,'male')
register('alex',73,'female')
register('wxx',84,'male')

2、默认参数的值只在定义阶段赋值一次,也就是说默认参数的值在定义阶段就固定死了
m = 10
def foo(x,y=m)
    print(x,y)
m=111111
foo(1)
>>>1,10
>

3、默认参数在传值的时候,不要将可变类型当作参数传值
def register(name,hobby,l=[]):
    l.append(hobby)
    print(name,l)
register('egon','play')
register('alex','AK')
register('wxx','gun')


def register(name,hobby,l=[]):
    l.append(hobby)
    print(name,l)
register('egon','play',[])  #egon ['play']
register('alex','AK',[])    #alex ['AK']
register('wxx','gun',[])    #wxx ['gun']


def register(name,hobby,l=None):
    if l == None:
        l=[]
        l.append(hobby)
        print(name,l)
register('egon','play')  #egon ['play']
register('alex','AK')   #alex ['AK']
register('wxx','gun')   #wxx ['gun']


Applications: For values ​​often need to change the corresponding parameter needs to be defined for the location parameter, for most of the values ​​are the same, it is necessary for the definition of the parameter to a default parameter.

4, the variable-length parameters : a variable length refers to the number of parameters may not be fixed. There are arguments arguments defined by location and by argument defined keywords, the variable-length argument refers to the real number of the parameter defined according to these two forms may not be fixed, but after all the argument to give pass parameter values, it must have a parameter corresponding to the two kinds of processing solutions to the two forms were more variable-length argument

foo(1,2)
foo(1,2,3)
foo(1,2,3,4)

foo(x=1,y=2)
foo(x=1,y=2,z=3)

Comprising parameter * / **

* Spill-over position to accept all the argument, and then stored in the form of the tuple is assigned to args


def foo(x,y,z,*args):
    print(x,y,z)
    print(args)
foo(1,2,3,4,5,6)
>>>123
>>>(4,5,6)
**会将溢出的关键字实参全部接受,然后保存成字典的形式赋值**后面的变量中
def foo(x,y,z,**kwargs):
    print(x,y,z)
    print(kwargs)
foo(x=1,y=2,z=3,d=4,f=5,e=6)
>>>1 2 3
>>>{'d': 4, 'f': 5, 'e': 6}

Argument contains * / **

*:一旦碰到实参加*,打散你传入的容器类型

def foo(x,y,z,*args):
    print(x,y,z)
    print(args)
foo(1,2,3,*[4,5,6])     #--->foo(1,2,3,4,5,6)
>>>1 2 3
>>>(4, 5, 6)
**:一旦碰到实参加**,就把字典打散成关键字参数传入形参

#定义函数
def test(*args, **kwargs):
    print(args)
    print(kwargs)
#定义一个元组和一个字典
tuple = (1,2,3,4,5)
dict = {'a':1,'b':2,'c':3}
#tuple和dict两个参数会作为一个元组传递给*args参数,而没有实参传递给**kwargs参数。
test(tuple,dict)
>>>((1, 2, 3, 4, 5), {'a': 1, 'b': 2, 'c': 3})
>>>{}
#打散传参
test(*tuple, **dict)  #test(1,2,3,4,5,a=1,b=2,c=3)
>>>(1, 2, 3, 4, 5)
>>>{'a': 1, 'b': 2, 'c': 3}

Combination: If a parameter is a function of shape and * args ** kwargs, then on behalf of the function can be received in any form, of any length parameters

def wrapper(*args,**kwargs):
    print(args)
    print(kwargs)
wrapper(1,2,3,4,5,6,x=6666,y=2222)
>>>(1, 2, 3, 4, 5, 6)
>>>{'x': 6666, 'y': 2222}

5, the keyword parameter named (Learn)
format: the back of the key parameters are named parameters
wherein:
parameter defined value must be delivered (with the exception of default values), and must be passed in the form of a keyword argument

def index(x, y, z, a=1, *args, b, **kwargs):
    print(x, y, z)
    print(a)
    print(args)
    print(b)
    print(kwargs)

index(1, 2, 3,354353, 4342, 3213123, b=222, c=333, d=444)
输出结果:
1 2 3
354353
(4342, 3213123)
222
{'c': 333, 'd': 444}

Guess you like

Origin www.cnblogs.com/baohanblog/p/12143035.html