****SPAM(7.4)**** Re: ****SPAM(11.2)**** [Tutor] Larger program
organization
Kent Johnson
kent37 at tds.net
Sun Feb 13 20:06:20 CET 2005
Bob Gailer wrote:
> At 03:21 PM 2/12/2005, Brian van den Broek wrote:
> [snip]
> > I am curious about Bob's "Whenever you find yourself writing
> > an if statement ask whether this would be better handled by subclasses."
>
> class A:
> ...
> class A1(A);
> def foo(self, ...):
> statements to process object of type 1
> class A2(A);
> def foo(self, ...):
> statements to process object of type 2
>
> That takes less code. Eliminates the type property. I get greater
> visibility about the existence and distinction of the two (or more)
> sub-types. I now can much more easily extend each subtype.
Also you can more easily *add* a new subtype. Using if statements, if you add a new type you have to
find all the relevant conditionals and add another condition. With subclassing, you create a new
subclass and define the necessary methods. The changes are localized to the subclass and much easier
to figure out.
Kent
More information about the Tutor
mailing list