0

I am trying to extend a dict using

dict(existing, **kwargs)

But this does not work when the kwargs are coming via another function. Can someone help me understand why is this so?

Sample test code:

def funct(*args, **kwargs):
    a = {'a': 1, 'b': 2}
    print kwargs
    return dict(a, **kwargs)

def thrgh(*v, **var):
    print var
    funct(*v, **var)

if __name__ == '__main__':
    print 'hello world'
    print funct(c=3)
    print 'helloWorld-thrg'
    print thrgh(c=3)

Output

hello world
{'c': 3}
{'a': 1, 'c': 3, 'b': 2}
helloWorld-thrg
{'c': 3}
{'c': 3}
None
Struggler
  • 672
  • 2
  • 9
  • 22

1 Answers1

1

You just need this:

def funct(*args, **kwargs):
a = {'a': 1, 'b': 2}
print (args)
print (kwargs)    
return dict(a, **kwargs)

funct('t', c=1)

This will return:

('t',)
{'c': 1}

{'a': 1, 'b': 2, 'c': 1}
prosti
  • 42,291
  • 14
  • 186
  • 151