Why a class when there will only be one instance?

SeeBelow at SeeBelow.Nut SeeBelow at SeeBelow.Nut
Wed May 26 13:37:09 EDT 2004


Peter Hickman wrote:
> 
> SeeBelow at SeeBelow.Nut wrote:
> > I see the value of a class when two or more instances will be created,
> > but Python programmers regularly use a class when there will only be one
> > instance.
> 
> This is a lot like putting code in functions. "Its only called in one place, why put it in a function, why not just leave it where it is?" I had a conversation with a dBase
> programmer many years ago on just these lines and what it came down to was this.
> 
> for x = 1 to length(orderitems)
>         calculate_tax(orderitems[x], .175)
> next
> 
> (forgive the pseudo code) is easier to read and the intent of the code is much clearer, and also we had 24 * 80 screens and so you could see much more of the 'flow' of the program
> than if the code was in lined. Objects just take all that one step further.
> 
> order.calculate_tax( .175 )
> 
> Why read fifty lines of code to work out that tax is being calculated for each item in the order when you can read one line? How you achieve this is not all that important, functions
> or classes only that the flow of the program can be comprehended quickly. 

I see the value of replacing many lines of code by a function
definition, for readability, even if that only happens once.  In fact I
regularly do this.  But does using a class acheive this better than a
function?  It seems to me that a function is simpler and clearer.

m

-- 
"Many are stubborn in pursuit of the path they have chosen, few in
pursuit of the goal." - Friedrich Nietzsche

http://annevolve.sourceforge.net is what I'm into nowadays.
Humans may write to me at this address: zenguy at shaw dot ca



More information about the Python-list mailing list