Classes as namespaces?

Jean-Michel Pichavant jeanmichel at sequans.com
Fri Mar 26 16:50:31 CET 2010


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



More information about the Python-list mailing list