I’m trying to ‘intercept’ all calls to a specific module, and reroute them to another object. I’d like to do this so that I can have a fairly simple plugin architecture.
For example, in main.py
import renderer
renderer.draw('circle')
In renderer.py
specificRenderer = OpenGLRenderer()
#Then, i'd like to route all calls from main.py so that
#specificRenderer.methodName(methodArgs) is called
# i.e. the above example would call specificRenderer.draw('circle')
This means that any function can just import renderer and use it, without worrying about the details. It also means that I can completely change the renderer just by creating another object and assigning it to the ‘specificRenderer’ value in renderer.py
Any ideas?
In
renderer.py:The module name is now mapped to the
OpenGLRendererinstance, andimport rendererin other modules will get the same instance.Actually, you don’t even need the separate module. You can just do:
… first thing in your main module. Imports of
rendererin other modules, once again, will refer to the same instance.Are you sure you really want to do this in the first place? It isn’t really how people expect modules to behave.