Late binding eval()?

xtian wilberforce at gmail.com
Wed Aug 25 01:05:39 EDT 2004


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

> and 'y' with their corresponding values and pass it in as the globals
or
> locals.  However, in my application, I'm getting an arbitrary
expression
> 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
they
> were called for, but that didn't work.
>

This will work in 2.4, see: http://www.python.org/2.4/highlights.html

"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."
Sweet.




More information about the Python-list mailing list