Evaluting a python code in a string

Paul Prescod paulp at ActiveState.com
Fri Jan 19 20:21:52 EST 2001


Steve Tuckner wrote:
> 
> 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? 

Others have described how to do this but I want to make the point that
it is often a feature that people use when they should not. It is
inefficient and also a potential security hole. Often people use it to
just convert types or delay executiion of some code. I don't know
anything about you so you may be an expert and know exactly what you are
doing but just in case not, I thought I'd give a warning.

 Paul Prescod




More information about the Python-list mailing list