python中*args和**kwargs

'''
*args:位置参数,位置很重要。列表形式
**kwargs:关键字参数,位置不重要。字典形式

Variables:
ls {list} -- [description]
dic {dict} -- [description]
foo(ls,dic) {[type]} -- [description]
'''

def foo(*args,**kwargs):
    print(*args)
    print(**kwargs)

ls = [1,2,3,4]
dic = {'a':'哈哈哈','b':'BBB'}
# foo(*ls,**dic)  # 错误用法,实际参数不要加*和**
foo(ls,dic)   # [1, 2, 3, 4] {'a': '哈哈哈', 'b': 'BBB'}

猜你喜欢

转载自www.cnblogs.com/wtgg/p/9782612.html
今日推荐