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.
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. Then the 1,2,3 and other "unnamed" arguments get stuffed into a tuple (I think?) for the single-star 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.