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

Nick Coghlan ncoghlan at
Wed Dec 8 12:57:52 CET 2004

It's me wrote:
> Yes, Russell, what you suggested works.
> I have to chew more on the syntax to see how this is working.
> because in the book that I have, it says:
>     exec code [ in globaldict [, localdict] ]

The [] indicate the last two parts are optional. If you don't supply them, exec 
just uses the current namespace.

However, as others have said, you are much better off using a separate 
dictionary for such values, rather than stuffing them into the local variables:

   user_vars = {}
   y = "a"
   user_vars[y] = 4

It keeps a clean separation between your variables and the user's variables, 
easily avoiding namespace conflicts.


Nick Coghlan   |   ncoghlan at   |   Brisbane, Australia

More information about the Python-list mailing list