class factory question

Peter Otten __peter__ at web.de
Wed Jun 26 11:40:44 EDT 2013


Joshua Landau wrote:

> I would say if a dict isn't good, there are still some cases where you
> might not want to use globals.
> 
> I _might_ do:

> # Make a module
> module_for_little_classes = ModuleType("module_for_little_classes",
> "All the things")
> module_for_little_classes.__dict__.update(little_classes)

Hm, from within module_for_little_classes that is globals(). To illustrate:

>>> import __main__ as main
>>> globals() is main.__dict__
True

Also, I'd spell module.__dict__ vars(module).

That said I agree that it's a good idea to use a dedicated module (not 
necessarily created on the fly) for those dynamically generated classes.




More information about the Python-list mailing list