量化交易之python基础篇 - 元祖、字典作为函数参数的应用

# 元祖作为函数参数
def sum_numbers(*args):
    sum = 0
    for item in args:
        sum += item
    return sum

print(sum_numbers(1, 2, 3, 4, 5)) # sum_numbers的函数参数因为是元祖, 所以理论上可以无限制增加参数的个数;
# 元祖和字典同时作为参数的调用实例
def show_list_dictionary(*args, **kwargs):
    print(args)
    print(kwargs)


show_list_dictionary(1, 2, 3, 4, name="roger", age=19) # 直接将元祖和字典的元素作为函数参数

foo_list = (1, 2, 3, 4, 5, 6, 7, 8)
roger_dictionary = {"name": "roger", "age": 25}
show_list_dictionary(*foo_list, **roger_dictionary)  # 将元祖和字典作为函数参数, *符号用来对列表解包,**符号用来对字典解包

猜你喜欢

转载自blog.csdn.net/Michael_234198652/article/details/109155243
今日推荐