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

Mike Meyer mwm at mired.org
Wed Aug 10 21:50:07 EDT 2005


Jeff Schwab <jeffrey.schwab at rcn.com> writes:

> 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

Jeff forgot to warn you that exec is a *dangerous* function. Don't use
it on data that you aren't 100% sure of the origin of. Better yet,
avoid it if at all possible.

If you'll tell us why you think you need to do this, possibly we can
help you avoid using exec.

     <mike
-- 
Mike Meyer <mwm at mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.



More information about the Python-list mailing list