Evaluting a python code in a string

Steve Tuckner sat at multitech.com
Fri Jan 19 11:47:01 EST 2001


In Perl you can evaluation perl code that is embedded in a string by using
the Eval function. For example the following code:

$i = "hello"
eval "print $i";

will print "hello" to the terminal.

How can a person do a similar function in Python? I tried the following
code:

import code
c = code.compile_command("print \"hello\"")
code.runcode(c)

and I get a run-time exception: Attribute error: runcode. I get the same
error if instead I just use the runsource() function.





More information about the Python-list mailing list