Reputation: 527
I would like tuple (foo, bar)
where
foo = 1
bar = 2
be converted to,
foo_dict = {'foo' : foo, 'bar' : bar}
Is there an easy way to do it?
Update:
I see that my original post is quite misleading. So, considering foo
and bar
are local variables within a function scope. I'd like them to be saved to a global dictionary where the keys are their variable names.
Re-update:
Thanks everyone for the attention. I was using ipywidgets.interact
to tune some parameters (passed as value), however I would like to save them globally, say in a dictionary. That's why the question comes in. Maybe for downvoters there may seem pointless to do such conversion, but do please share some hints for solve my needs pythonically.
Upvotes: 2
Views: 1187
Reputation: 1053
The required function call is
def func(**kwargs):
for key in kwargs.keys():
globals()[key]=kwargs[key]
return kwargs
foo_dict = func(foo = 1, bar = 2)
print(foo_dict)
print(foo)
print(bar)
#{'bar': 2, 'foo': 1}
#56
#hehe
Upvotes: 1