Classes as namespaces?

Jonathan Hartley tartley at tartley.com
Sat Mar 27 12:28:56 CET 2010


On Mar 26, 6:26 pm, Luis M. González <luis... at gmail.com> wrote:
> On 26 mar, 11:49, kj <no.em... at please.post> 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)
>
> I see no problem.
> I wouldn't mix English, French and Spanish in the same recipe though...


Hey everyone. By coincidence, only yesterday I was wondering about
using classes as a way of labeling a block of code, ie. an lightweight
alternative to defining a function that would only be called from one
location.

eg. instead of:


x = 1
((some complex logic))
y = 2


one might like to name the complex block of logic, just to make it
readable:


x = 1
def account_for_non_square_pixels(x):
   ((some complex logic))
account_for_non_square_pixels()
y = 2


But defining and then calling the function like that is a tad
cumbersome. So I was wondering about:



x = 1
class account_for_non_square_pixels:
  ((some complex logic))
y = 2


I don't exactly like this, but I think you can see what I'm getting
at. Does this fall down in some way I haven't grasped? Is it as awful
an abuse of 'class' as my intuition suggests it is? Is there a way to
do it better?



More information about the Python-list mailing list