suppose I have a class which has 10 methods,
(i write pass
here, but assume they would have some implementation)
class KlassOne:
def method_one(self, x):
pass
def method_two(self, y, z):
pass
...
def method_five(self, a):
pass
...
def method_ten(self, b):
pass
and a second class which inherits from the first one.
class KlassTwo(KlassOne):
def method_eleven(self, w):
pass
but KlassTwo
does not want all ten methods of KlassOne
,
let us say KlassTwo
wants to inherit only these four methods,
wanted_methods = [method_one, method_three, method_eight, method_nine]
and the rest are not applicable for KlassTwo
one example could be,
KlassOne
is Person
and KlassTwo
is Robot
and method_five
is EatsFood
so, our Robot
does not want to inherit EatsFood
whereas method_one
is BodyWeight
, and let us assume it makes sense for both Person
and Robot
, so Robot
wants to inherit method_one
.
but how could this partial inheritance be achieved???
one way to do this is by using NotImplemented
, for example,
class KlassTwo(KlassOne):
def method_five(self, a):
raise NotImplemented
and do the same for each method that is not wanted.
or the other way could be to use Composition, like,
class KlassTwo:
def __init__(self, x):
self.t = KlassOne.method_one(self, x)
something like that, and only use the methods that are wanted.
but I would like to use inheritance, and completely disable the inheritance of some methods, that is something like,
class KlassOne:
@not_inheritable
def method_five(self, a):
pass
so that no subclass would get method_five
.
how do I achieve this?
or give a list in KlassTwo
, again like,
wanted_methods = [method_one, method_three, method_eight, method_nine]
and ensure that only these get inherited.