Is there a way of executing a command in a string?

Jeff Schwab jeffrey.schwab at rcn.com
Tue Aug 9 22:58:24 EDT 2005


Jerry He wrote:
> Hi, 
>   suppose I have the following string
> 
> cmdstr = "b = lambda s: s*s"
> 
>   Is there a way to execute this string other than
> copying it onto a file and then importing it? 

 >>> exec "b = lambda s: s*s"
 >>> b
<function <lambda> at 0x4d69cc>



More information about the Python-list mailing list