Late binding eval()?

Diez B. Roggisch deetsNOSPAM at web.de
Tue Aug 24 17:33:04 EDT 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:
    try:
        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.

-- 
Regards,

Diez B. Roggisch



More information about the Python-list mailing list