HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Structure of inheritance between Animal classes and methods

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
animalinheritancemethodsbetweenstructureclassesand

Problem

I'm trying to do some basic inheritance between the 3 classes and use some simple methods, and I'm looking to learn what should be the correct structure between them and what can be improved.

class Animal:
    def __init__(self, kind, place):
        self.kind = kind
        self.place = place

#This class inherits kind and place arguments from Animal class (which can work for any animal)
#Initiates a Dog object with name, gender and breed parameters.

class Dog(Animal):
    def __init__(self, name, gender, breed):
        self.name = name
        self.gender = gender
        self.breed = breed
        Animal.__init__(self, 'Dog', 'Ground')

    #woof method, just prints an action for the Dog object with his name. 
    def woof(self, *args):
        print("%s just did a %s woof" % (self.name, *args))

    #getallinfo method, get's all the parametrs of both classes.
    def getallinfo(self):
        print("%s is a %s %s %s, sitting on the %s" % (self.name, self.gender, self.breed, self.kind, self.place))

#Cat class inherits the paramets of use for a Cat (similar things) like name, gender and breed, which they both share, also the getallinfo method and initiate them. 
class Cat(Dog):
    def __init__(self, name, gender, breed):
        Dog.__init__(self, name, gender, breed)
        Animal.__init__(self, 'Cat', 'Ground')

#Speak method, returns a print for a cat "meow" with him name.
    def speak(self, *args):
        print("%s just did a %s meow" % (self.name, *args))

#Here I create 3 objects, 2 dogs and 1 cat with selected arguments.
#And check for some methods on the objects.
Mickey = Dog('Mickey', 'Male', 'Bulldog')
Flora = Dog('Flora','Female','Pug')
Tina = Cat('Tina','Female','Persian')
Tina.getallinfo()
Tina.speak('soft')
Dog.getallinfo(Flora)
Dog.woof(Mickey, 'loud')

Solution

Inheritance is typically describe as an "is-a" relationship. So when you derive a Dog from an Animal, we can say that a Dog is a Animal. However, when you derive a Cat from a Dog, unless this is some other planet, we can't correctly say that a Cat is a Dog. Further, we can, without error, invoke Tina.woof('ferocious') to cause "Tina just did a ferocious woof". Since no Persian cat I've ever seen has been known to "woof" either ferociously or not, this is an alarming and surprising result.

Better would be to derive both Dog and Cat types from Animal. If you have other animals which don't have names, breeds or genders, you could have some intermediate class such as Pet that would capture the additional detail not already in Animal. Otherwise, just add those attributes to Animal.

Finally, we can put the speak method in the base Animal class. One simple illustration is this:

class Animal:
    def __init__(self, kind, place):
        self.kind = kind
        self.place = place

    def speak(self, adverb):
        print("%s just did a %s %s" % (self.name, adverb, self.speechtype))

    #getallinfo method, get's all the parametrs of both classes.
    def getallinfo(self):
        print("%s is a %s %s %s, sitting on the %s" % (self.name, self.gender, self.breed, self.kind, self.place))

#This class inherits kind and place arguments from Animal class (which can work for any animal)
#Initiates a Dog object with name, gender and breed parameters.

class Dog(Animal):
    def __init__(self, name, gender, breed):
        self.name = name
        self.gender = gender
        self.breed = breed
        self.speechtype = "woof";
        Animal.__init__(self, 'Dog', 'Ground')

#Cat class inherits the paramets of use for a Cat (similar things) like name, gender and breed, which they both share, also the getallinfo method and initiate them. 
class Cat(Animal):
    def __init__(self, name, gender, breed):
        self.name = name
        self.gender = gender
        self.breed = breed
        self.speechtype = "meow";
        Animal.__init__(self, 'Cat', 'Ground')

#Here I create 3 objects, 2 dogs and 1 cat with selected arguments.
#And check for some methods on the objects.
Mickey = Dog('Mickey', 'Male', 'Bulldog')
Flora = Dog('Flora','Female','Pug')
Tina = Cat('Tina','Female','Persian')
Tina.getallinfo()
Tina.speak('soft')
Flora.getallinfo()
Mickey.speak('loud')


There's more that could be improved, but I hope that helps until others weigh in on your question and give more expansive answers.

Code Snippets

class Animal:
    def __init__(self, kind, place):
        self.kind = kind
        self.place = place

    def speak(self, adverb):
        print("%s just did a %s %s" % (self.name, adverb, self.speechtype))

    #getallinfo method, get's all the parametrs of both classes.
    def getallinfo(self):
        print("%s is a %s %s %s, sitting on the %s" % (self.name, self.gender, self.breed, self.kind, self.place))

#This class inherits kind and place arguments from Animal class (which can work for any animal)
#Initiates a Dog object with name, gender and breed parameters.

class Dog(Animal):
    def __init__(self, name, gender, breed):
        self.name = name
        self.gender = gender
        self.breed = breed
        self.speechtype = "woof";
        Animal.__init__(self, 'Dog', 'Ground')


#Cat class inherits the paramets of use for a Cat (similar things) like name, gender and breed, which they both share, also the getallinfo method and initiate them. 
class Cat(Animal):
    def __init__(self, name, gender, breed):
        self.name = name
        self.gender = gender
        self.breed = breed
        self.speechtype = "meow";
        Animal.__init__(self, 'Cat', 'Ground')


#Here I create 3 objects, 2 dogs and 1 cat with selected arguments.
#And check for some methods on the objects.
Mickey = Dog('Mickey', 'Male', 'Bulldog')
Flora = Dog('Flora','Female','Pug')
Tina = Cat('Tina','Female','Persian')
Tina.getallinfo()
Tina.speak('soft')
Flora.getallinfo()
Mickey.speak('loud')

Context

StackExchange Code Review Q#145740, answer score: 5

Revisions (0)

No revisions yet.