Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python call a function with kwargs

I have a function:

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

and my newfunc

def newfunc(**kwargs):
    print a

Its not giving the value of a which is 1

whats wrong ?

like image 492
varad Avatar asked Sep 14 '25 13:09

varad


1 Answers

It's because you didn't put any key, value in your dictionary, you should have written that :

def newfunc(**kwargs):
    print kwargs["a"]

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

You can refer to this thread to understand kwargs better : Understanding kwargs in Python

like image 94
Luc DUZAN Avatar answered Sep 17 '25 04:09

Luc DUZAN