Python function parameters: positional parameters, default parameters, variable parameters, keyword parameters, named keyword parameters

First of all, it must be clear that the typical data structures in python are tuple, list, set and dictionary

list = [1, 2, 3]
tuple = (1, 2, 3)
dictionary = {'Michael': 95, 'Bob': 75, 'Tony':85}

#简化可变参数的调用方法
def calc(*numbers):
    sum = 0
    for n in numbers:
        sum = sum + n * n
    return sum

variable parameter

Can simplify the calling method of variable parameters

Keyword parameter **kw

def person(name, age, **kw):
    print('name:', name, 'age:', age, 'other:', kw)

>>> person('Bob', 35, city='Beijing')
name: Bob age: 35 other: {'city': 'Beijing'}
>>> person('Adam', 45, gender='M', job='Engineer')
name: Adam age: 45 other: {'gender': 'M', 'job': 'Engineer'}

#简化调用方法
>>> extra = {'city': 'Beijing', 'job': 'Engineer'}
>>> person('Jack', 24, **extra)
name: Jack age: 24 other: {'city': 'Beijing', 'job': 'Engineer'}

Keyword parameters can be entered or not entered, and they are automatically blocked from being a dictionary

Named keyword parameters

Only accept cityand jobas keyword parameters. The functions defined in this way are as follows:

def person(name, age, *, city, job):
    print(name, age, city, job)

*The following parameters are treated as named keyword parameters

If the function has been defined in a variable parameter, followed later named keyword arguments no longer need a special separator *of

def person(name, age, *args, city, job):
    print(name, age, args, city, job)

Named keyword parameters must be passed in the parameter name, which is different from positional parameters. If no parameter name is passed in, the call will report an error!

Named keyword parameters can have default values ​​to simplify the call:

def person(name, age, *, city='Beijing', job):
    print(name, age, city, job)
#由于命名关键字参数city具有默认值,调用时,可不传入city参数
>>> person('Jack', 24, job='Engineer')
Jack 24 Beijing Engineer

Parameter combination

To define a function in Python, you can use mandatory parameters, default parameters, variable parameters, keyword parameters, and named keyword parameters. All 5 parameters can be used in combination. But please note that the order of parameter definition must be: mandatory parameters, default parameters, variable parameters, named keyword parameters, and keyword parameters.

def f1(a, b, c=0, *args, **kw):
    print('a =', a, 'b =', b, 'c =', c, 'args =', args, 'kw =', kw)

def f2(a, b, c=0, *, d, **kw):
    print('a =', a, 'b =', b, 'c =', c, 'd =', d, 'kw =', kw)

>>> f1(1, 2)
a = 1 b = 2 c = 0 args = () kw = {}
>>> f1(1, 2, c=3)
a = 1 b = 2 c = 3 args = () kw = {}
>>> f1(1, 2, 3, 'a', 'b')
a = 1 b = 2 c = 3 args = ('a', 'b') kw = {}
>>> f1(1, 2, 3, 'a', 'b', x=99)
a = 1 b = 2 c = 3 args = ('a', 'b') kw = {'x': 99}
>>> f2(1, 2, d=99, ext=None)
a = 1 b = 2 c = 0 d = 99 kw = {'ext': None}

>>> args = (1, 2, 3, 4)
>>> kw = {'d': 99, 'x': '#'}
>>> f1(*args, **kw)
a = 1 b = 2 c = 3 args = (4,) kw = {'d': 99, 'x': '#'}
>>> args = (1, 2, 3)
>>> kw = {'d': 88, 'x': '#'}
>>> f2(*args, **kw)
a = 1 b = 2 c = 3 d = 88 kw = {'x': '#'}

For any function, it can be func(*args, **kw)called in a similar form, no matter how its parameters are defined.

Reference source: https://www.liaoxuefeng.com/wiki/1016959663602400/1017261630425888

Guess you like

Origin blog.csdn.net/li4692625/article/details/109482654