By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,676 Members | 2,249 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,676 IT Pros & Developers. It's quick & easy.

dynamic function

P: n/a
Hello pythonistas,

I have programmed a product in Zope which allows me to source code on the
web.
Now I want to compile the code into a function and execute the function.
The code is as follows:

import new
code = compile(srcString, 'getContent', 'exec')
f = new.function(code, globalDict, 'getContent')

If srcString contains a return statement, then
python returns a CompilationError stating, that I cannot use return outside
a function.
How do I have to compile the sourcecode to get around this problem?

--
Mit freundlichen Grüssen
Xaver Hinterhuber
Jul 18 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a

"Xaver Hinterhuber" <xa***************@web.de> wrote in message
news:c7*********@online.de...
Hello pythonistas,

I have programmed a product in Zope which allows me to source code on the
web.
Now I want to compile the code into a function and execute the function.


If the source is a comple def statement with a know name...
exec "def f(): return 'this works'"
f()

'this works'

Terry J. Reedy


Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.