class derived from dict in eval

Aahz aahz at pythoncraft.com
Fri Feb 28 15:35:56 EST 2003


In article <nNw6a.308773$AA2.11749597 at news2.tin.it>,
Alex Martelli  <aleax at aleax.it> wrote:
>
>You have to prepare a real dictionary for the names used
>in the expression you're interested in, because, as others
>said responding to you, the LOAD_NAME operation bypasses
>polymorphic behavior and goes for actual dict item access.
>
>Fortunately, it's not too hard:
>
>def evalWithSpecialDict(expression, specialDict):
>    compiled = compile(expression, '<string>', 'eval')
>    realDict = {}
>    for varname in compiled.co_names:
>        realDict[varname] = specialDict[varname]
>    return eval(compiled, realDict)

You're sick.  You need help.
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

Register for PyCon now!  http://www.python.org/pycon/reg.html




More information about the Python-list mailing list