Late binding eval()?

xtian wilberforce at
Wed Aug 25 07:05:39 CEST 2004

Kevin Smith wrote:
> I want to evaluate an expression using eval(), but I only want to
> the variable values "on demand."  For example, let's say that I want
> evaluate 'x+y'.  Normally, I would create a dictionary containing 'x'

> and 'y' with their corresponding values and pass it in as the globals
> locals.  However, in my application, I'm getting an arbitrary
> from the user.  I thought that I could just create a dictionary-like
> object and override __getitem__ to look up values in a database as
> were called for, but that didn't work.

This will work in 2.4, see:

"eval() now accepts any form of object that acts as a mapping as its
argument for locals, rather than only accepting a dictionary. There's
all sorts of new and shiny evil possible thanks to this little change."

More information about the Python-list mailing list