Refactoring similar subclasses
Bruno Desthuilliers
bruno.42.desthuilliers at websiteburo.invalid
Mon Sep 13 05:39:09 EDT 2010
Steven D'Aprano a écrit :
> I have some code that currently takes four different classes, A, B, C and
> D, and subclasses each of them in the same way:
>
> class MyA(A):
> def method(self, x):
> result = super(MyA, self).method(x)
> if result == "spam":
> return "spam spam spam"
> return result
> # many more methods overloaded
>
>
> class MyB(B):
> def method(self, x):
> result = super(MyB, self).method(x)
> if result == "spam":
> return "spam spam spam"
> return result
> # many more methods overloaded
>
If that's really what your code is doing - I mean, calling the super()
implementation, checking the result and eventually returning something
else instead - then a simple decorator might be enough... Could even be
"applied" by the metaclass.
More information about the Python-list
mailing list