Execute external code and get return value

Michele Petrazzo michele.petrazzoDELETE at DELETEunipex.it
Fri Sep 29 08:53:54 EDT 2006


Michele Petrazzo wrote:

Auto reply:

> I find this: http://tinyurl.com/nwbpk
> 

Following this link, here is my solution:


code = """
def funct2Call():
  return "It work!"

object.method(funct2Call)
"""

class Object(object):
     def method(self, value):
         self._callable = value
     def __call__(self):
         return self._callable()

# populate the namespace
namespace = {
     "object": Object()
     }

# execute the code
exec code in namespace

namespace["object"]()

Michele



More information about the Python-list mailing list