[英]Passing list and dictionary type parameter with Python
When I run this code 当我运行此代码时
def func(x, y, *w, **z):
print x
print y
if w:
print w
if z:
print z
else:
print "None"
func(10,20, 1,2,3,{'k':'a'})
I get the result as follows. 我得到的结果如下。
10 20 (1, 2, 3, {'k': 'a'}) None
But, I expected as follows, I mean the list parameters (1,2,3) matching *w, and dictionary matching **z. 但是,我的预期如下,我的意思是匹配* w的列表参数(1,2,3)和字典匹配** z。
10 20 (1,2,3) {'k':'a'}
Q : What went wrong? 问:出了什么问题? How can I pass the list and dictionary as parameters?
如何将列表和字典作为参数传递?
func(10,20, 10,20,30, k='a')
seems to be working 好像在起作用
在字典前放两个星号:
func(10,20, 1,2,3,**{'k':'a'})
I'm not sure what the "input" format is, but this will work: 我不确定“输入”格式是什么,但这样可行:
func(10,20, 1,2,3, k='a')
With this, you don't even need to put the k=a out there at the end, it can be anywhere after the first two arguments. 有了这个,你甚至不需要在最后把k = a放在那里,它可以在前两个参数之后的任何地方。 Then the 1,2,3 and other "unnamed" arguments get stuffed into a tuple (I think?) for the single-star arg.
那么1,2,3和其他“未命名的”参数会被塞进一个元组(我认为?)中,用于单星arg。
If you want to be extra explicit, you can do 如果你想要更加明确,你可以做到
func(10,20,*[1,2,3],**{'k':'a'})
to specify (to the reader) which argument you want to go with each special-form parameter. 指定(对于读者)您要使用每个特殊形式参数的参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.