An inheritance question: getting the name of the "one up" class

Nick mediocre_person at hotmail.com
Tue Mar 31 05:13:44 CEST 2009


I've got a collection of classes describing animals, part of which looks 
like:

class Animal(object):
    def __init__(self):
        self.pet = False
        self.edible = False
        self.legs = 0
        self.sound = None
        self.name = self.__class__.__name__.lower()

class Mammal(Animal):
    def __init__(self):
        Animal.__init__(self)
        self.legs = 4

class Primate(Mammal):
    def __init__(self):
        Mammal.__init__(self)
        self.legs = 2

class Human(Mammal):
    def __init__(self):
        Primate.__init__(self)
        self.sound = "Hey, I can talk!"

I want to add a "pedigree" function to Animal so that I can have:

>>> h = Human()
>>> h.pedigree()
human < primate < mammal < animal
>>>

I've been thinking about something like:
def pedigree(self):
    n = self.name
    while n != 'object' # base class of Animal
        print n,
        n = Well, this is where I'm stuck. super(???,???).???
    print

Oh, and while the gurus are at it, what would be the advantage (if any) of 
changing, say
   Primate.__init__(self)
to
    super(Human, self).__init__()

Thanks, all,

Nick. 




More information about the Python-list mailing list