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

1

3 Answers 3

7

Put two asterisks before the dictionary:

func(10,20, 1,2,3,**{'k':'a'})
Sign up to request clarification or add additional context in comments.

1 Comment

This gives what the OP wants.
2

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.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.