Classes as namespaces?
Ethan Furman
ethan at stoneleaf.us
Sat Mar 27 09:34:45 EDT 2010
Jonathan Hartley wrote:
> 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?
Both solutions look horrible to me, as both hurt readability. Make your
function somewhere else, then call it in the code. Who cares if you
only use it once?
x = 1
account_for_non_square_pixels()
y = 2
Isn't that easier to read?
And when you want to (read/change) the complex code, you have an easy
place to go to do it.
~Ethan~
More information about the Python-list
mailing list