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

Nick Coghlan ncoghlan at iinet.net.au
Wed Dec 8 06:57:52 EST 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.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at email.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://boredomandlaziness.skystorm.net



More information about the Python-list mailing list