Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

packing named arguments into a dict

Tags:

I know I can turn function arguments into a dictionary if the function takes in **kwargs.

def bar(**kwargs):
    return kwargs

print bar(a=1, b=2)
{'a': 1, 'b': 2}

However, is the opposite true? Can I pack named arguments into a dictionary and return them? The hand-coded version looks like this:

def foo(a, b):
    return {'a': a, 'b': b}

But it seems like there must be a better way. Note that i am trying to avoid using **kwargs in the function (named arguments work better for an IDE with code completion).


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!