Вопрос

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)
Это было полезно?

Решение

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)

Другие советы

Just as it accepts them:

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)
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top