Evaluting a python code in a string

Emile van Sebille emile at fenx.com
Fri Jan 19 11:51:33 EST 2001


>>> a = 'print "hello"'
>>> exec(a)
hello
>>>

--

Emile van Sebille
emile at fenx.com
-------------------


"Steve Tuckner" <sat at multitech.com> wrote in message
news:Se_96.394$mY4.179630 at news.uswest.net...
> 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