Suppose I have some variables:
a, b, c, d, e = range(5)
I want to save the values of these variables in a file for later inspection. One way I thought to do this was:
lookup = {
'a': a,
'b': b,
'c': c,
'd': d,
'e': e
}
As you might imagine, with a large number of variables, this could get tedious. And, yes, I know many editors have functionality to make this kind of copy-paste action easy. But I'm looking for the standard, "Pythonic" way of dynamically building a key: value lookup where the key is the variable's name and the value is the variable's, well, value!
I thought about this:
>>> {var.__name__: var for var in [a, b, c, d, e]}
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 1, in <dictcomp>
AttributeError: 'int' object has no attribute '__name__'
I'm not surprised this didn't work, because integer variables are constants (I'm not sure of the exact way to describe things):
>>> a = 1
>>> b = 1
>>> a is b
True
>>> b is a
True
>>> a == b
True
How might I accomplish this?