How do I do this? (eval() on the left hand side)
caleb1 at telkomsa.net
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'
'>>> d = locals() # Get a dictionary of local variables
'>>> 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
> 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
> you wish to evaluate the expression (like a calculator application, for
More information about the Python-list