Python 可变长参数*arg **kwargs
在Python中,有两种变长参数,分别是元组(非关键字参数)和字典(关键字参数),其参数以一个开头表示任意长度的元组[tuple],可以接收连续一串参数,参数以两个开头表示一个字典[dict],即”key:value”,接受连续任意多个参数。
1###示例函数###
2def Variable(*arg,**kwargs):
3 print arg
4 print kwargs
5 print "\n"
6###执行输出tuple###
7>>> Variable(1,2,3,4,5,"www","9i-it","org")
8(1, 2, 3, 4, 5, 'www', '9i-it', 'org')
9{}
10###执行输出dict###
11>>> Variable(www=1,iit=2,org=3)
12()
13{'org': 3, 'iit': 2, 'www': 1}
14##一起来##
15>>> Variable(1,2,3,4,5,www=1,iit=2,org=3)
16(1, 2, 3, 4, 5)
17{'org': 3, 'iit': 2, 'www': 1}
捐赠本站(Donate)
如您感觉文章有用,可扫码捐赠本站!(If the article useful, you can scan the QR code to donate))
- Author: shisekong
- Link: https://blog.361way.com/python-variable-arg-kwargs/4094.html
- License: This work is under a 知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议. Kindly fulfill the requirements of the aforementioned License when adapting or creating a derivative of this work.