Why a class when there will only be one instance?

Peter Hickman peter at semantico.com
Wed May 26 11:42:47 EDT 2004


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. Objects don't just make writing code easier but it makes *READING* code easier too, a very important plus 
(ever had to maintain APL?)

Almost everything that I write becomes an object even the programs themselves.



More information about the Python-list mailing list