How do I do this? (eval() on the left hand side)

Caleb Hattingh caleb1 at
Wed Dec 8 05:28:39 CET 2004

Sure, ok, I think I am with you now.

You get a (e.g.) variable name as a string, and you KNOW how to evaluate  
it with "eval", but you also want to be able to assign back to (through)  
the string representation?

One way (if I understand you correctly) is with the globals or locals  
dicts.  Try this in IDLE:

'>>> a = 3
'>>> y = 'a'
'>>> eval(y)
'>>> d = locals() # Get a dictionary of local variables
'>>> d['a']
'>>> d[y]
'>>> d[y] = 8   # y is a string = 'a'
'>>> a              # The value of a is changed.

Is this kinda what you mean?  I'm still new at this (and don't know REXX  
 from Adam).


> There are many situations where this is useful.   For instance, you  
> might be
> getting an input which is a string representing the name of a variable  
> and
> you wish to evaluate the expression (like a calculator application, for
> instance).

More information about the Python-list mailing list