简体   繁体   中英

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

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)

正如它接受它们一样:

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)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM