Function Input arg, args, kwargs usage

  • Reference material
    1. https://www.cnblogs.com/yunguoxiaoqiao/p/7626992.html
    2. https://www.jianshu.com/p/98f7e34845b5
  • Thus advantage may not necessarily function input and output will be set forth in a number beginning with the number of input by using the function of the flexibility.
  • Python range order parameter input function, the parameters must be defined: mandatory parameter, default parameter, the variable parameter / parameter named keywords and keyword parameters.

variable parameter

def calc(*numbers):
    sum = 0
    for n in numbers:
        sum = sum + n * n
    return sum
print(calc(1,2,3,4))
30

Keyword arguments

def person(name, age, **kw):
    print('name:', name, 'age:', age, 'other:', kw)
print(person('Bob', 35, city='Beijing'))
name: Bob age: 35 other: {'city': 'Beijing'}
None
def test(*a, **kw):
    sum_t = 0
    for n in a:
        sum_t += n
    for i,j in kw.items():
        sum_t += j
    return sum_t
print(test(1,2,3,identy = 4))
10
Published 36 original articles · won praise 0 · views 20000 +

Guess you like

Origin blog.csdn.net/weixin_38102912/article/details/100664859