Reputation: 10109
I have the following classes:
class A:
def __init__(self,x):
self.x = x
self.xp = self.preprocess(x)
def preprocess(self,x):
return(x**2)
class B(A):
def __init__(self,x,y):
A.__init__(self,x)
self.y = y
def results():
return(self.xp + self.y)
In this toy example, A
is used to read some data (x
) and preprocess them (xp
).
B
further reads some data (y
) and produces a result using the preprocessed data from A
.
Now I need to create a class C
. C
has exactly the same difference as B
, so I am tempted to
simply define it as:
class C(B):
pass
however, I need to override the A.preprocess()
method, so that the correct preprocessing is used when C
is constructed. Is there an easy and straightforward way to do this?
Upvotes: 0
Views: 159
Reputation: 1121554
Yes, just define a new preprocess
method on C
:
class C(B):
def preprocess(self, x):
return x + x
The A.__init__()
method will find that method over the one defined in A
when called with type(self) is C
.
Upvotes: 2