Build classes/packages dinamicaly

Michele Simionato michele.simionato at poste.it
Mon Dec 15 13:25:22 EST 2003


Paulo Pinto <paulo.pinto at cern.ch> wrote in message news:<brkkf7$jqg$1 at sunnews.cern.ch>...
> Hi,
> 
> 
> I have a package that generates classes from a
> set of XML files using exec.
> 
> So far the classes appear in the global namespace.
> 
> Is there any way to also create packages dinamicaly
> and add the classes to those packages?
> 
> Thanks in advance,
> Paulo Pinto

By packages I think you mean modules. Here is a solution in Python 2.3:

>>> from types import ModuleType
>>> mymodule=ModuleType("mymodule")
>>> print mymodule
<module 'mymodule' (built-in)>
>>> class C(object): pass
... 
>>> mymodule.C=C

In older Python versions, look for the module "new".




More information about the Python-list mailing list