Chaz Ginger wrote: > I was writing some code that used someone else class as a subclass. He > wrote me to tell me that using his class as a subclass was incorrect. I > am wondering under what conditions, if ever, does a class using a > subclass not work. [snip] > He said I should use it this way: > > class B2: > def __init__(self,a1,a2): > self.c = a1 > self.t = A(a2) > > def bar(self) : > self.t.bar() > > Other than the obvious difference of B2 having an attribute 't', I can't > see any other obvious differences. Is there something I am missing?
I think it's kind of a fine point. In my own code I've had cases where I've switched from subclass to attribute and back over the development, and vice versa. I think there are many cases where it's preferable to use an attribute, but not really wrong to subclass (and vice versa). The classical advice in choosing whether to subclass or or use attribute is whether its more an an "is a" or "has a" relationship. If it's more natural to say B is an A, then subclass. If it's more natural to say B has an A, then use an attribute. But that's only a rule of thumb. I personally find another question helpful. If it's reasonable that B could have more than one of A, regardless if it actually does, use an attribute. If it's unreasonable, subclass. Again, rule of thumb. Carl Banks -- http://mail.python.org/mailman/listinfo/python-list