eval vs. exec

Simon Budig Simon.Budig at unix-ag.org
Mon May 27 14:07:30 EDT 2002


holger krekel <pyth at devel.trillke.net> wrote:
> what you can do is e.g.

[slightly adjusted]

import code

def run_code(string, mathstuff={'myabs': lambda x: abs(x)}):
   interpreter = code.InteractiveInterpreter()
   interpreter.locals = mathstuff
   codeobj = code.compile_command(string)
   result = interpreter.runcode(codeobj)
   return result

print "Result: %s" % repr (run_code('a=2'))
print "Result: %s" % repr (run_code('3*4'))
print "Result: %s" % repr (run_code('a=2;b=3;a*b'))
print "Result: %s" % repr (run_code('a=-3;myabs(a*5)'))

and this results in:

Result: None
12
Result: None
6
Result: None
15
Result: None

As you can see the return value always is "None". The real results
get printed to stdout. This is the same problem I had with the
eval/exec stuff.

Thanks for trying  :-)
        Simon
-- 
      Simon.Budig at unix-ag.org       http://www.home.unix-ag.org/simon/



More information about the Python-list mailing list