测开之路八十一:参数定义之*args和**kwargs

 

# *,不定长参数,*args
# 定义函数参数
def avg(score, *scores):
    return (score + sum(scores)) / (len(scores) + 1)


# 调用传参一:直接传参
print(avg(123, 234, 234, 123, 123))

# 调用传参二:解包传参
nums = (123, 234, 234, 123, 123)
print(avg(*nums))

 

# **, 关键字参数,key-value的形式,**kwargs
# 定义函数参数
def display(**employee):
    print(employee)


# 传参那方式一:直接传参
display(name='tom', age=22, job='dev')

# 传参那方式二:解包传参
data = {
    'name': 'tom',
    'age': 22,
    'job': 'dev'
}
display(**data)

 

posted @ 2019-08-27 15:15  向前走。  阅读(185)  评论(0编辑  收藏  举报