Late binding eval()?

Diez B. Roggisch deetsNOSPAM at
Tue Aug 24 23:33:04 CEST 2004

Kevin Smith wrote:
> Does anyone have any ideas on how to do this?

A hack-approach to this migth be to eval in a loop and catch the NameError,
extract the name and bind it like this:

exp = "x * y + z"

z = 10

run = True
while run:
        print eval(exp)
        run = False
    except NameError, e:
        name = e.args[0].split("'")[1]
        globals()[name] = 10

Of course you can prompt the user for input, or collect the names and try to
get them at once from whereever.


Diez B. Roggisch

More information about the Python-list mailing list