Dynamic/runtime code introspection/compilation

Fredrik Lundh fredrik at pythonware.com
Tue Nov 28 08:27:36 EST 2006


Thomas W wrote:

> from somemodule import ISomeInteface
> 
> d = compile(sourcecode)
> 
> myfoo = d.Foo()
> 
> print ISomeInterface in myfoo.__bases__
> 
> Any hints?

Python is a dynamic language, so compiling something won't tell you much 
about what the code actually does.  the only reliable way to do that is 
to execute the code:

     code = compile(sourcecode)
     namespace = {}
     exec code in namespace
     print issubclass(namespace["Foo"])

</F>




More information about the Python-list mailing list