Returning a value from code string

Steven D'Aprano steve at
Sat Jan 28 00:33:48 EST 2006

On Fri, 27 Jan 2006 20:33:53 -0800, Kirk McDonald wrote:

> Say I have a database containing chunks of Python code. I already have a 
> way to easily load, edit, and save them. 


> What is slightly baffling to me 
> is how I can effectively pass this code some arguments, run it, and 
> somehow get a return value.

You already know about exec. Do you know about eval?

result = eval(some_piece_of_text)

I hope the contents of the database are trusted, because if the code is
coming from an untrusted source, well, U R pwn3d.

Seems like you are jumping through a lot of hoops for very little benefit.
What am I missing?


More information about the Python-list mailing list