Classes as namespaces?

Harishankar v.harishankar at gmail.com
Fri Mar 26 11:04:13 EDT 2010


On Fri, 26 Mar 2010 14:49:02 +0000, 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.
> 
> 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.

I myself am a humble beginner in many ways, but generally isn't that 
(namespacing) achieved by using modules?

I don't find the need generally to assign namespace to local variables 
and when there is a need for it, module level objects do the job.



More information about the Python-list mailing list