Classes as namespaces?

kj no.email at please.post
Fri Mar 26 10:49:02 EDT 2010



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.



More information about the Python-list mailing list