python call a function with kwargs -


i have function:

def myfunc():     kwargs = {}     = 1     b = 2     kwargs.update(a=a, b=b)     newfunc(**kwargs) 

and newfunc

def newfunc(**kwargs):     print 

its not giving value of 1

whats wrong ?

two things.

first, kwargs argument in myfunc empty dict, won't pass parameters. if want pass value of a , b newfunc, can either use

kwargs = {'a':1, 'b':2} newfunc(**kwargs) 

or

newfunc(a=1, b=2) 

in both cases, 'a' , 'b' in kwargs dict of newfunc function.

second, should extract argument kwargs dict in newfunc. print kwargs.get('a') should suffice.


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -