When is a subclass not right?

Chaz Ginger cginboston at hotmail.com
Thu Aug 24 16:44:12 EDT 2006


Gabriel Genellina wrote:
> At Thursday 24/8/2006 16:23, 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.
>>
>> class B1(A);
>>   def __init__(self,a1,a2) :
>>         self.c = a1
>>         A.__init__(self,ag)
>>
>> 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?
> 
> Look any OO book for the difference between 'inheritance' and 
> 'delegation'. In short, you should inherit when B 'is an' A (a Car is a 
> Vehicle), and delegate/compose in other cases (a Car has an Engine; or 
> more precisely, a Car instance has an Engine instance).
> 
> 
> Gabriel Genellina
> Softlab SRL
> 
>     
> p5.vert.ukl.yahoo.com uncompressed Thu Aug 24 19:27:05 GMT 2006     
>        
> __________________________________________________ Preguntá. Respondé. 
> Descubrí. Todo lo que querías saber, y lo que ni imaginabas, está en 
> Yahoo! Respuestas (Beta). ¡Probalo ya! http://www.yahoo.com.ar/respuestas

That is merely a logical use of OO after all when would a car and an 
orange be the same?

I was wondering more about the mechanics of Python: when does B1 show 
different characteristics than B2  (forgoing the obvious simple things, 
like 't' above).

Chaz




More information about the Python-list mailing list