Can I build an automatic class-factory that works on import? <p>I have a class-factory F that generates classes. It takes no arguments other than a name. I'd like to be able to wrap this method and use it like this:</p> <pre><code>from myproject.myfactory.virtualmodule import Foo </code></pre> <p>"myfactory" is a real module in the project, but I want virtualmodule to be something that pretends to be a module.</p> <p>Whenever I import something from virtualmodule I want it to build a new class using my factory method and make it look as if that was imported. </p> <p>Can this be done? Is there a pattern that will allow me to wrap a class-factory as a module?</p> <p>Thanks!</p> <p>--</p> <p>UPDATE0: Why do I need this? It's actually to test a process that will be run on a grid which requires that all classes should be importable.</p> <p>An instance of the auto-generated class will be serialized on my PC and then unserialized on each of the grid nodes. If the class cannot be imported on the grid node the unserialize will fail. </p> <p>If I hijack the import mechanism as an interface for making my test-classes, then I know for sure that anything I can import on my PC can be re-created exactly the same on the grid. That will satisfy my test's requirements.</p>