class factory question

Joshua Landau joshua.landau.ws at gmail.com
Wed Jun 26 19:20:20 EDT 2013


On 26 June 2013 16:40, Peter Otten <__peter__ at web.de> wrote:
> 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

Yes, that's true - but the point wasn't not to use "globals the
function", but not to use *this* global scope.

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

Again, good catch. Definitely that.

> 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