I would like to write a class that is basically an add-on to existing classes. It should be designed to be only inherited and not work on its own. So far I am using something like this example.
class Swordsman: # First self sufficient class
def __init__(self):
pass
def walk(self):
pass
def cover(self):
print('Covering behind my shield')
class Wizard: # Second self sufficient class
def __init__(self):
pass
def walk(self):
pass
def cover(self):
print('Covering behind my robe')
class HealerAddon: # addon class
def __init__(self):
pass
def heal(self):
self.cover() # this is ugly
print('healing')
class Paladin(Swordsman,HealerAddon):
def __init__(self):
Swordsman.__init__(self)
HealerAddon.__init__(self)
class Druid(Wizard,HealerAddon):
def __init__(self):
Wizard.__init__(self)
HealerAddon.__init__(self)
if __name__ == '__main__':
arthos = Paladin()
arthos.heal()
druid1 = Druid()
druid1.walk()
druid1.heal()
In the example Swordsman and Wizard are two self suffient classes. HealerAddon is an Addon class that can be used to add healing capability to either of the former two. The result are the two classes Paladin and Druid, which are a healing swordsman and wizard respectiveley.
So far this works but I am not really happy with this way of doing it especially because of the first line in the heal method, where the existance of a cover method is assumed although it becomes only clear in the combined classes Druid and Paladin, where cover is coming from.
Is there a better way of doing this?