separate instances of modules

Helge Hess helge at mdlink.de
Wed Oct 27 17:00:14 EDT 1999


Hi,

I'm thinking about some integration issues between a C environment I use
and Python modules. It works like this, that one can extend the
environment by writing components in Python where each component is
assigned exactly one Python source file, that is, a module. Right now
such a file looks like:

MyComponent.py:

  import stuff;
  class MyComponentClass:
     def a(self):
           self.b = 5

When such a component is instantiated in the C environment, the source
is imported (via Py_Import) or reloaded and then an instance of the
MyComponentClass is made.

Since the source file contains always one such class (which doesn't need
to inherit) I wonder whether it's somehow possible to use the source
itself as the class. That is, I would like to write all the class stuff
directly in the source (as functions and module variables), like this:

   import stuff;
   b = 3;
   def a():
     b = 5;

This way it would be much more convenient. The problem I have right now
is that when I import a module, I always get the same instance which
isn't ok for me since I have multiple instances of one component
"class".

Does anybody know how to create multiple 'module' instances with
separate environments, eg:

  PyObject *c1 = Py_ImportAsNew("MyComponent.py");
  PyObject *c2 = Py_ImportAsNew("MyComponent.py");

?

Thankful for any suggestions,
  Helge




More information about the Python-list mailing list