When is a subclass not right?
Chaz Ginger
cginboston at hotmail.com
Thu Aug 24 15:23:08 EDT 2006
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.
Here is an example. For instance the original class might look like:
class A :
def __init__(self,arg) :
self.foo = arg
def bar(self) :
return self.foo
And I defined a class B1 which looked like:
class B1(A);
def __init__(self,a1,a2) :
self.c = a1
A.__init__(self,ag)
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?
TIA
Chaz
More information about the Python-list
mailing list