On Sat, Mar 27, 2010 at 04:28:56AM -0700, Jonathan Hartley wrote regarding Re: Classes as namespaces?: > > 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? > -- > http://mail.python.org/mailman/listinfo/python-list
Hmm. I don't like that because it leaves a class polluting your namespace that doesn't behave like a class. Using a function for that purpose doesn't seem as bad, because even if you don't call it again, at least you *could*, and it would behave in an expected fashion. If you're dead-set against calling the chunk of code you just created, and you're using python 2.5 or higher, you might consider creating a no-op context manager: x = 1 with code_block("Account for non square pixels"): ((complex_logic)) y = 2 Though in general, I think refactoring your code to reasonably scoped functions or methods is a better idea. If it's too complex to read in one block, it's probably too complex for one function. -- http://mail.python.org/mailman/listinfo/python-list