Home > Blockchain >  Code for an inheritance diagram in python
Code for an inheritance diagram in python

Time:01-22

I'm learning OOP with python and wanted to write code for an inheritance diagram I found online to practice. This is the diagram:

enter image description here

This is my code:

#parent class that is used to set food and family variables for child classes
class Animal:
    def __init__(self,family,food):
        self.family = family
        self.food = food
    
    def getFamily(self):
        return self.family
    def setFamily(self,newFamily):
        self.family = newFamily
    def getFood(self):
        return self.food
    def setFood(self,newFood):
        self.food = newFood

#child class that inherits from Animal class
class Cow(Animal):
    def __init__(self,owner, family,food):
        self.owner = owner
        #use the Animal init function to set the family and food properties
        Animal.__init__(self,family,food)
    def setOwner(self,newOwner):
        self.owner = newOwner
    def getOwner(self):
        return self.owner

class Lion(Animal):
    def __init__(self,family,food):
        Animal.__init__(self,family,food)
    
mooingCow = Cow("Bob", "mammal","grass")
print(Cow.__name__ "'s owner: "   mooingCow.getOwner()   ", family: "   mooingCow.getFamily())
hungryLion = Lion("mammal","humans")

My request is, can anyone comment on the correctness of my solution and help me improve it where I can? Also when I removed the Animal parameter from Cow class definition I expected the code to throw an error as Cow is no longer inheriting from Animal so it should not have access to the properties and methods defined in it, however, the code still executes fine. Please could someone also explain why this is happening to me?

Where I expected the error to happen:

class Cow():
    def __init__(self,owner, family,food):
        self.owner = owner
        #use the Animal init function to set the family and food properties
        Animal.__init__(self,family,food) # <--- Where I expected the error to happen

CodePudding user response:

class Animal(object):
    def __init__(self,family,food):
        self.family = family
        self.food = food
    
    def getFamily(self):
        return self.family
    def setFamily(self,newFamily):
        self.family = newFamily
    def getFood(self):
        return self.food
    def setFood(self,newFood):
        self.food = newFood

#child class that inherits from Animal class
class Cow(Animal):
    def __init__(self,owner, family,food):
        self.owner = owner
        #use the Animal init function to set the family and food properties
        super().__init__(family,food)
    def setOwner(self,newOwner):
        self.owner = newOwner
    def getOwner(self):
        return self.owner

class Lion(Animals):
    pass
    
mooingCow = Cow("Bob", "mammal","grass")
print(Cow.__name__ "'s owner: "   mooingCow.getOwner()   ", family: "   mooingCow.getFamily())
hungryLion = Lion("mammal","humans")

You can define class Animals as an object to make it a parent class, and you can remove Animal parameter from the Cow class definition, as python will automatically inherit the parent class.

If the Animal class is defined before the Cow class, and Animal class is defined in the same file, python interpreter bypass the error.

In this case, the interpreter recognizes that Animal class defined earlier in the script and will use it as the parent class for the Cow class. However, this is not considered a good practice as it can lead to unexpected behavior if the order of the classes is changed in the script or if the script is imported into another script.

However if you say:

>>> cow = Cow('a', 'b', 'c')
>>> cow.getFamily()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Cow' object has no attribute 'getFamily'
  • Related