Last active
March 7, 2016 04:35
-
-
Save aaronchall/ae173ef9343f30d10a28 to your computer and use it in GitHub Desktop.
Python implementation of multiple inheritance for The Codeless Code: Case 83 Consequences
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import print_function | |
class Soldier(object): | |
def __init__(self, rank='noob'): | |
self.rank = rank | |
def fight(self, foe): | |
'''abstract method? fights to death implementation''' | |
class Archer(Soldier): | |
def __init__(self, rank='noob', arrows=100, **kwargs): | |
super(Archer, self).__init__(rank=rank, **kwargs) | |
self.arrows = arrows | |
def fight(self, enemies): | |
for foe in enemies: | |
self.shoot(foe) | |
self.arrows -= 1 | |
super(Archer, self).fight(foe) | |
def shoot(self, foe): | |
print('{0} shoots {1} w/ arrow {2}'.format(self.rank, foe, self.arrows)) | |
class Horseman(Soldier): | |
def __init__(self, rank='noob', horse=None, **kwargs): | |
self.horse = horse or 'any horse' | |
super(Horseman, self).__init__(rank=rank, **kwargs) | |
def fight(self, enemies): | |
self.trample(enemies) | |
super(Horseman, self).fight(enemies) | |
def trample(self, enemies): | |
print('{0} tramples {1} w/ {2}'.format(self.rank, enemies, self.horse)) | |
class FRoF(Horseman, Archer): | |
def __init__(self, rank='noob', arrows=100, horse=None): | |
super(FRoF, self).__init__(rank=rank, arrows=arrows, horse=horse) | |
def fight(self, enemies): | |
self.charge(enemies) | |
super(FRoF, self).fight(enemies) | |
def charge(self, enemies): | |
print('{0} leads charge against {1}'.format(self.rank, enemies)) | |
def getmembers(obj): | |
return [i for i in dir(obj) if not i.startswith('_')] | |
def main(): | |
FRoF('leet', horse='Atreyu', arrows=500).fight('abcde') | |
Horseman('amateur').fight('abc') | |
Archer().fight('cde') | |
print('FRoF can', getmembers(FRoF)) | |
print('Horseman can', getmembers(Horseman)) | |
print('Archer can', getmembers(Archer)) | |
print('Soldier can', getmembers(Soldier)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment