0

In the following code, how do I pass the dictionary to func2. How should func2 be called?

def func2(a,**c):
 if len(c) > 0:
   print len(c)
   print c

u={'a':1,'b':2}
func2(1,u)
2
  • Your for loop still won't work. Get rid of the and 'a' in c and it would, not sure what that clause is trying to do? Commented Sep 27, 2011 at 6:14
  • @agf:i haqve modified the code ,now am just printing c Commented Sep 27, 2011 at 6:20

3 Answers 3

2

Just as it accepts them:

func2(1,**u)
Sign up to request clarification or add additional context in comments.

3 Comments

additionally: def func2(a,c): ... and func2(1,u) would also work. using variable arguments or standard arguments depends on what you are trying to achieve.
func2(1,**u) i get an error saying Typeerror:func2() got multiple values for keyword argument 'a'
I got the above error because dictionary had the key 'a' and one of the parameters was also 'a'. when i changed it the error got resolved..
1

This won't run, because there are multiple parameters for the name a.

But if you change it to:

def func2(x,**c):
    if len(c) > 0:
    print len(c)
        print c

Then you call it as:

func2(1, a=1, b=2)

or

u={'a':1,'b':2}
func2(1, **u)

1 Comment

ya true i realized and have updated in of the comment.so parameters names and the dict key are not suppose to be the same.Thanks..
0

This might help you:

 def fun(*a, **kw):
     print a, kw

 a=[1,2,3]
 b=dict(a=1, b=2, c=3)

 fun(*a)
 fun(**kw)
 fun(*a, **kw)

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.