Function basis - vararg

Vararg: refers to the function call, the number of parameters passed may not be fixed

The function is called, the value of no more than two pass mode, one is the position of the argument, and the other argument is a keyword, and therefore must have formal parameters there are two solutions, in order to receive the overflow location argument (*) with the keyword arguments (**)

A variable length parameter of Dian *

* Parameter will overflow in the position-all argument, and then stored tuple, the tuple is then assigned to the parameters *. Note that: * the name of the parameter convention for the args.

def sum_self(*args):
    res = 0
    for num in args:
        res += num
    return res


res = sum_self(1, 2, 3, 4)
print(res)

10


Variable-length argument of two Dian *

The arguments * * * value will be taken out cycling parameters, broken into position argument. After arguments whenever encountered with *, it is the position argument should be broken into a position immediately see argument.

def func(x, y, z, *args):
    print(x, y, z, args)


func(1, *(1, 2), 3, 4)

1 1 2 (3, 4)


Wed and variable length parameter of **
parameter ** in the spill-over-all key argument, and then stored in the dictionary, then the dictionary assigned to the parameters **. Note that: ** parameter name after the convention was kwargs.

def func(**kwargw):
    print(kwargw)

func(a=5)

{'a': 5}


Dian four arguments of a variable length
argument is
, it will be the value of the parameter after the extraction cycle, break the keyword argument. After whenever encountered, it is the key arguments in arguments with ** should immediately broken into keyword arguments to see.

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

func(1, 3, 4, **{'a': 1, 'b': 2})

1 3 4 {'a': 1, 'b': 2}


Dian five variable-length parameters are applied

def index(name, age, sex):
    print(f"name: {name}, age: {age}, sex: {sex}")

def wrapper(*args, **kwargs):
    print(f"args: {args}")
    print(f"kwargs: {kwargs}")
    index(*args, **kwargs)

wrapper(name='nash', sex='male', age=18)

args: () kwargs: {'name': 'nash', 'sex': 'male', 'age': 18} name: nash, age: 18, sex: male


Dian named six key parameter

There is now a demand: the user function argument must pass by keyword.

def register(x, y, **kwargs):
    if 'name' not in kwargs or 'age' not in kwargs:
        print('用户名和年龄必须使用关键字的形式传值')
        return
    print(kwargs['name'])
    print(kwargs['age'])


register(1, 2, name='nash', age=18)

nick 19
Name Keyword parameter: function definition stage, parameters are named after the * key parameter.

Features: In the pass value must be passed in accordance with the value of key = value manner and key must be named keyword arguments specified parameter name.

def register(x, y, *, name, gender='male', age):
    print(x)
    print(age)


register(1, 2, x='nash', age=18)  # TypeError: register() got multiple values for argument 'x'

Guess you like

Origin www.cnblogs.com/suren-apan/p/11374797.html