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

Jeff Schwab jeffrey.schwab at
Wed Aug 10 04:58:24 CEST 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