1

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.

MeadowMuffins
  • 507
  • 1
  • 5
  • 20
  • 1
    A tuple contains values, not variables. There is absolutely no connection between those values and the variables that held them; indeed, the value might never have been held in a variable at all, or might have been held by multiple variables. – jasonharper Apr 27 '17 at 16:08
  • @jasonharper I see, forgiving tuple, how could we do so for several variables then? If we have a dozen? – MeadowMuffins Apr 27 '17 at 16:10
  • What is the bigger problem? – Elmex80s Apr 27 '17 at 16:11
  • Actually I think this can be done, I just have to put the pieces together. I'm currently testing. – roganjosh Apr 27 '17 at 16:12
  • @MeadowMuffins Why do you need the `foo_dict`? – frederick99 Apr 27 '17 at 16:12
  • I think your main problem is [getting the names of variables as strings](http://stackoverflow.com/questions/2553354/how-to-get-a-variable-name-as-a-string-in-python). – Christian König Apr 27 '17 at 16:13
  • @frederick99 I want to save multiple local variables in a global dictionary. – MeadowMuffins Apr 27 '17 at 16:13
  • @MeadowMuffins thing is, they are already in a global dictionary. `print globals()` or `print vars()` or `print locals()`. What I've just found out though is that this really doesn't work in Enthought Canopy, which might be due to iPython. – roganjosh Apr 27 '17 at 16:16
  • @frederick99 I am now try to pass them like `def func(**kwargs)` so to get over this [getting the names of variables as strings](http://stackoverflow.com/questions/2553354/how-to-get-a-variable-name-as-a-string-in-python). – MeadowMuffins Apr 27 '17 at 16:18
  • Your goal is getting _more_ confusing to me. Can you please edit the question to actually state what the overall aim of this is? What use do you have for such strings? Currently it looks like you're creating problems for yourself. – roganjosh Apr 27 '17 at 16:20
  • Is that a regular dictionary you want to add to or the `globals()` dictionary? – frederick99 Apr 28 '17 at 19:21

1 Answers1

1

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
frederick99
  • 1,033
  • 11
  • 18