Re: ****SPAM(7.4)**** Re: ****SPAM(11.2)**** [Tutor] Larger program organization
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 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: ****SPAM(7.4)**** Re: ****SPAM(11.2)**** [Tutor] Larger program organization
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." I start out writing a class like: class A: def __init__(self, type): self.type = type ... def foo(self, ...): if self.type = 1: statements to process object of type 1 else: statements to process object of type 2 The '"if statement" alerts me to consider creating subclasses for types 1 and 2: 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. Bob Gailer mailto:[EMAIL PROTECTED] 303 442 2625 home 720 938 2625 cell ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor