Dynamic/runtime code introspection/compilation

Thomas W thomas.weholt at gmail.com
Tue Nov 28 07:35:23 EST 2006


Maybe a stupid subject, but this is what I want to do :

I got some python code stored in a string:

somecode = """

from somemodule import ISomeInterface

class Foo(ISomeInterface):
    param1 = ...
    param2 = ....

"""

and I want to compile that code so that I can use the Foo-class and
check what class it extends, in this case ISomeInterface etc. I've
tried eval, codeop etc. but it doesn't work. Something like this would
be nice :

from somemodule import ISomeInteface

d = compile(sourcecode)

myfoo = d.Foo()

print ISomeInterface in myfoo.__bases__

Any hints?




More information about the Python-list mailing list