Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

I am new to python and wanted to learn it so I started learning from a week and today I took a topic called inheritance. I got this piece of code through the internet and tried to understand it. The code is as follows:

class Animal:

    __name = ""

    __height = 0

    __weight = 0

    __sound = 0

    #constructor

    def __init__(self, name, height, weight, sound):

        self.__name = name

        self.__height = height

        self.__weight = weight

        self.__sound = sound

    def set_name(self,name):

        self.__name = name

    def get_name(self):

        return self.__name

    def set_height(self,height):

        self.__height = height

    def get_height(self):

        return self.__height

    def set_weight(self,weight):

        self.__weight = weight

    def get_weight(self):

        return self.__weight

    def set_sound(self,sound):

        self.__sound = sound

    def get_sound(self):

        return self.__sound

    def get_type(self):

        print("Animal")

    def toString(self):

        return "{} is {} cm tall and {} kg and says 

{}".format(self.__name,self.__height,self.__weight,self.__sound)

human = Animal("Rover",55,25,"woof")

print(human.toString())

#Inheritance

class Dog(Animal):

    __owner = "" #Inherit all variables from Animal class. Add an owner 

 variable. Every dog class has an owner variable, but not every

             #animal class has an owner variable.

    #overwrite the constructor 

    def __init__(self, name, height, weight, sound, owner):

        self.__owner = owner

        super(Dog,self).__init__(name, height, weight, sound) #let Animal 

superclass handle the other variables.

    def set_owner(self,owner):

        self.__owner = owner

    def get_owner(self):

        return self.__owner

    def get_type(self):

        print("Dog")

    def toString(self):

        return "{} is {} cm tall and {} kg and says {}. His owner is 

{}".format(self.__name,self.__height,self.__weight,self.__sound,self.__owner)

 myDog = Dog("Rover",55,25,"woof","Alex")

 print(myDog.toString())

When I executed the code I am getting error as shown:

AttributeError: 'Dog' object has no attribute '_Dog__name'

As per my knowledge Dog subclass is not properly inheriting the variables from the Animal superclass?

I am using Python 3.6 with Spyder 3.2.3

1 Answer

0 votes
by (36.8k points)

After going through the code I have noticed that you are referring to java code and trying to make java code work on Python. That's the reason you are getting the error

This is the code to be used on the Spyder IDE

class Animal:

    def __init__(self, name, height, weight, sound):

        self.name = name

        self.height = height

        self.weight = weight

        self.sound = sound

    def __str__(self):

        return "{} is {} cm tall and {} kg and says {}".format(self.name,self.height,self.weight,self.sound)

class Dog(Animal):

    def __init__(self, name, height, weight, sound, owner):

        self.owner = owner

        super(Dog,self).__init__(name, height, weight, sound)

    def __str__(self):

        return "{} is {} cm tall and {} kg and says {}. His owner is  {}".format(self.name,self.height,self.weight,self.sound,self.owner)

my_dog = Dog("Rover",55,25,"woof","Alex")

print(my_dog)

If you are a beginner and want to know more about Data Science the do check out the Data Science course

Related questions

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...