Classes as namespaces?

Chris Colbert sccolbert at gmail.com
Fri Mar 26 13:36:55 EDT 2010


i use them in Pymazon to encapsulate program wide settings and enforce valid
values for these settings.
http://code.google.com/p/pymazon/source/browse/pymazon/settings.py



On Fri, Mar 26, 2010 at 11:50 AM, Jean-Michel Pichavant <
jeanmichel at sequans.com> wrote:

> kj wrote:
>
>> What's the word on using "classes as namespaces"?  E.g.
>>
>> class _cfg(object):
>>    spam = 1
>>    jambon = 3     huevos = 2
>>
>> breakfast = (_cfg.spam, _cfg.jambon, _cfg.huevos)
>>
>>
>> Granted, this is not the "intended use" for classes, and therefore
>> could be viewed as a misuse ("that's what dictionaries are for",
>> etc.).  But other than this somewhat academic objection[*], I really
>> can see no problem with using classes in this way.
>>
>>
> You cannot see the problem because there's no problem using classes as
> namespaces.
>
>  And yet, I've come across online murky warnings against using
>> classes as "pseudo-namespaces".  Is there some problem that I'm
>> not seeing with this technique?
>>
>> ~K
>>
>>
> import this
> [snip]
> Namespaces are one honking great idea -- let's do more of those!
>
> Modules and dictionaries are no more namespaces than classes. So any
> container is potentially a namespace.
>
> JM
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100326/7adfc6ee/attachment-0001.html>


More information about the Python-list mailing list