Classes as namespaces?

Jon Clements joncle at
Fri Mar 26 16:47:29 CET 2010

On 26 Mar, 14:49, kj <no.em... at> 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.
> 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
> [*] My own subjective dislike for the widespread practice of using
> triple quotes to comment out code is formally similar to this one
> ("the 'intended use' for triple-quoting is not to comment out code",
> etc.).  Here I find myself on the opposite side of the purist/pragmatic
> divide.  Hmmm.

Given this example, I would go for the module and CONSTANT_NAMING

But yes, even in the docs. you can use a class as a C type-of struct.

I stick to the convention of a class knows what it's doing,
what it's doing it on, and a module just happens to contain those

C++ std::algorithm for instance,
makes sense it's called std, ditto algorithm and has shed loads in it,
but would I create a class called algorithm (unlikely).

I would tend to view modules as "namespace". Rightly or wrongly, just
lets you make the right design choice.


More information about the Python-list mailing list