Build classes/packages dinamicaly

Peter Otten __peter__ at web.de
Mon Dec 15 11:00:02 EST 2003


Paulo Pinto wrote:

> 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

>>> import types
>>> mymodule = types.ModuleType("mymodule")
>>> exec "def demo():\n\tprint 'hello from', __name__\n" in
mymodule.__dict__
>>> mymodule.demo()
hello from mymodule
>>>

Seems to work. I haven't used it myself, though.

Peter





More information about the Python-list mailing list