views:

118

answers:

3

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?

Added

func(10,20, 10,20,30, k='a')

seems to be working

+7  A: 

Put two asterisks before the dictionary:

func(10,20, 1,2,3,**{'k':'a'})
Daniel Stutzbach
This gives what the OP wants.
Michael Herold
+1  A: 

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.

Michael Herold
+2  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.

dash-tom-bang