python function indefinite length parameter

 

def fun(a, b, *args):
    print(a)
    print(b)
    print(args)
    print("="*30)
    ret = a + b
    for i in args:
        ret += i
    return ret

print(fun(1,2,3,4))

结果:
1
2
(3, 4)
==============================
10

 

1 and 2 are assigned to a and b respectively, and the remaining parameters are assigned to args in the form of tuples

 

Dictionary formal parameters:

 

def fun(a, b, *args, **kwargs):
    print(a)
    print(b)
    print(args)
    print(kwargs)

fun(1, 2, 3, 4, name = "hello", age = 20)

结果:
1
2
(3, 4)
{'name': 'hello', 'age': 20}

 

 

Pass in tuples and dictionaries:

 

def fun(a, b, *args, **kwargs):
    print(a)
    print(b)
    print(args)
    print(kwargs)

tup = (11,22,33)
dic = {"name":"hello", "age":20}
fun(1, 2, *tup, **dic)

结果:
1
2
(11, 22, 33)
{'name': 'hello', 'age': 20}

 

Guess you like

Origin blog.csdn.net/qq_17758883/article/details/104653388