Home Classes and inheritance in Python 3.6 (Spyder)
Reply: 0

Classes and inheritance in Python 3.6 (Spyder)

user2331 Published in July 19, 2018, 11:13 pm

I have beginner Python question. I have a superclass called "Animal", and I'm using inheritance to create a "Dog" class.

 class Animal:
    __name = ""
    __height = 0
    __weight = 0
    __sound = 0

    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):

    def toString(self):
        return "{} is {} cm tall and {} kg and says 

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


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):

    def toString(self):
        return "{} is {} cm tall and {} kg and says {}. His owner is 

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


I am getting the following error:

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

Is the Dog subclass somehow not able to inherit the name variable from the Animal superclass?

I'm using Python 3.6 with Spyder 3.2.3

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.324788 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO