class Animal(object):
def __init__(self, *args, **kwargs):
self.objName = kwargs['objName']
self.isAnimal = True
self.animalStr = "%s is not a programmer." %(self.objName)
def getIsAnimal(self):
return self.isAnimal
def describeAnimal(self):
print (self.animalStr)
class Mammal(Animal):
def __init__(self, *args, **kwargs):
if kwargs['b_prop']:
# use b_prop to call every superclass __init__
# at the same time, avoid duplicated calls
kwargs['b_prop'] = False
for eachSuperClass in Mammal.__mro__[-2:0:-1]:
# reverse because a subclass could define attributes more accurately
eachSuperClass.__init__(self, **kwargs)
self.objName = kwargs['objName']
self.isMammal = True
self.mamStr = "%s indeed is a mammal." %(self.objName)
def getIsMammal(self):
return self.isMammal
def describeMammal(self):
print (self.mamStr)
class LandedAnimal(Animal):
def __init__(self, *args, **kwargs):
if kwargs['b_prop']:
kwargs['b_prop'] = False
for eachSuperClass in LandedAnimal.__mro__[-2:0:-1]:
eachSuperClass.__init__(self, **kwargs)
self.objName = kwargs['objName']
self.isOnLand = True
self.landedStr = "%s lives happily on land." %(self.objName)
def getIsOnLand(self):
return self.isOnLand
def describeOnLand(self):
print (self.landedStr)
class LeggedAnimal(LandedAnimal):
def __init__(self, *args, **kwargs):
if kwargs['b_prop']:
kwargs['b_prop'] = False
for eachSuperClass in LeggedAnimal.__mro__[-2:0:-1]:
eachSuperClass.__init__(self, **kwargs)
self.objName = kwargs['objName']
self.legCnt = kwargs['legCnt']
self.legStr = "%s indeed has %s leg(s)." %(self.objName, self.legCnt)
def getLegCnt(self):
return self.legCnt
def describeLeg(self):
print (self.legStr)
class DogPlus(Mammal, LeggedAnimal):
def __init__(self, *args, **kwargs):
if kwargs['b_prop']:
kwargs['b_prop'] = False
for eachSuperClass in DogPlus.__mro__[-2:0:-1]:
eachSuperClass.__init__(self, **kwargs)
self.objName = kwargs['objName']
self.greetingStr = kwargs['greetingStr']
self.farewellStr = kwargs['farewellStr']
def greeting(self):
print (self.greetingStr)
def farewell(self):
print (self.farewellStr)
macconnell = DogPlus(b_prop=True, \
objName="MacConnell", legCnt=4, \
greetingStr="Hi there sweety.", farewellStr="Till next time sweety.")
if macconnell.isAnimal:
macconnell.describeAnimal() # It moves too!
if macconnell.isMammal:
macconnell.describeMammal() # MacConnell is indeed a mammal.
if macconnell.isOnLand:
macconnell.describeOnLand() # MacConnell lives happily on land.
if macconnell.legCnt:
macconnell.describeLeg() # MacConnell indeed has 4 leg(s).
else:
print ("NO LEGS!")
macconnell.greeting() # Hi there sweety.
macconnell.farewell() # Till next time sweety.
0 / 960