Re: Dynamic/runtime code introspection/compilation



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>

.



Relevant Pages

  • Re: Project organization and import
    ... I feel like the Python package system, ... Namespace. ... filesystem heirarchy. ... or forced to use "from foo import *". ...
    (comp.lang.python)
  • Re: How to import only one module in a package when the package __init__.py has already imports the
    ... Do you understand that each function has it's own local namespace? ... This is a constrain imposed by python. ... Because of this constraint in python. ... move it to a better package). ...
    (comp.lang.python)
  • Re: creating class objects inside methods
    ... Say you went to another country, where the people wore lead shoes, ... Python's namespace and object models, and so you have little standing ... the Python maintainers are very particular to define "bug" as ... And if you say, "Well you should be able to customize assigment", my ...
    (comp.lang.python)
  • Re: import vs from module import : any performance issue?
    ... When a line of code asks for the value of a variable x, Python will search ... local namespace - specific to the current function or class method. ... this and stop searching. ... > subsequent imports are essentially lookups in sys.modules. ...
    (comp.lang.python)
  • Re: Command Line Compilation - Inherits
    ... If you don't stipulate either Public or Private, ... > Members of a module are accessible within a namespace without specifying ... > Remember that even when compiling assemblies that contain VB.NET ...
    (microsoft.public.dotnet.framework.aspnet)