开发者

Passing a dictionary as a function parameter and calling the function in Python

开发者 https://www.devze.com 2023-04-09 04:16 出处:网络
In the following code, how do I pass the dictionary to func2. How should func2 be called? de开发者_如何学运维f func2(a,**c):

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

de开发者_如何学运维f func2(a,**c):
 if len(c) > 0:
   print len(c)
   print c

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


Just as it accepts them:

func2(1,**u)


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)


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)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号