Python方法参数*args和**kwargs总结(详细案例)

在阅读python代码的时候经常可以看到*keys和 **kwargs一直弄不明白现在明白了


参数中传入元组,args表示成一个元组

def param_tuple(*args):
    print(args)
    for value in args:
        print(value)

    print(enumerate(args)) #object
    for key,value in enumerate(args):
        print("{0}->{1}".format(key,value))
param_tuple(1,2,43)
'''
(1, 2, 43)
1
2
43

0->1
1->2
2->43
'''

参数中传入元组,args表示成一个字典

def param_dic(**kwargs):
    print(type(kwargs))
    for name,age in kwargs.items():
        print("{0}==>{1}".format(name,age))

param_dic(name="小明",age=20)
'''
(1, 2, 43)
1
2
43

0->1
1->2
2->43
'''

函数的参数中混用tuple和dic

def param_tuple_dic(*args,**kwargs):
    print(type(args))
    print(type(kwargs))
    for value in args:
        print(value)

    for key ,value in kwargs.items():
        print("{0}==>{1}".format(key,value))

param_tuple_dic("1","2","3",name="小明",age="10")

'''


1
2
3
name==>小明
age==>10
'''

你可能感兴趣的:(python爬虫,经验交流,Python)