-3

I am supposed to create a class called Dog(Animal) which is inheriting from Class Animal. However after I run this code, I got errors that I do not understand:

The problem is solved, no further questions that I see at the moment

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 get_height(self):
    return str(self.__height)

def get_weight(self):
    return str(self.__weight)

def get_sound(self):
    return self.__sound

def get_type(self):
    print("Animal")

def toString(self):
    return "{} is {} cm tall and {} kilograms and says{}".format(self.__name,
                                                                 self.__height,
                                                                 self.__weight,
                                                                 self.__sound)


cat = Animal('pussy', 33, 10, 'meow')
print(cat.toString())
print(cat.get_type())
print(cat.get_sound())

class Dog(Animal):
__owner = ""

def __init__(self, name, height, weight, sound, owner):
    self.__owner = owner
    super(Dog, self).__init__(name, height, weight, sound)

def set_owner(self, owner):
    self.__owner = owner

def get_owner(self):
    return self.__owner

def get_type(self):
    print("Dog") # Dog object

def toString(self):
    return "{} is {} cm tall and {} kilograms and says{} its owner is {}".format(self.__name,
                                                                                 self.__height,
                                                                                 self.__weight,
                                                                                 self.__sound,
                                                                                 self.__owner)

spot = Dog("kaili", 22, 33, "woof", "Jiahui") 
print(spot.toString())  
Hanshenry90
  • 109
  • 1
  • 8
  • 1
    Please provide the error stack trace as a code snippet instead of linking to an image. – Timotheus.Kampik Apr 08 '17 at 09:17
  • Hi there, do you mean by traceback? I attached a picture in the post. Sorry I am a newbie. – Hanshenry90 Apr 08 '17 at 18:16
  • @Hanshenry: for text artefacts like code and console IO, readers do not like images. Images are not compatible with clipboards, search engines and screen-readers. Would you delete that image and replace it with text? Use code formatting for logs too. – halfer Apr 10 '17 at 13:29
  • With regret, I have downvoted (see above). I will undownvote if you can repair the question. Thanks! – halfer Apr 14 '17 at 20:07
  • I have deleted the picture, and I have found the answer. – Hanshenry90 Apr 14 '17 at 23:12

2 Answers2

1

Change this line:

class Animal:

to this:

class Animal(object):

This post explains it quite well.

Community
  • 1
  • 1
t.m.adam
  • 15,106
  • 3
  • 32
  • 52
  • Hi, I looked at error: super(Dog, self).__init__(name, height, weight, sound) TypeError: super() argument 1 must be type, not classobj I am confused the super(Dog, self) is not working – Hanshenry90 Apr 09 '17 at 23:38
  • Did you read the [post](http://stackoverflow.com/questions/4015417/python-class-inherits-object) ? Have you modified your code ? – t.m.adam Apr 09 '17 at 23:41
  • Hi i changed the parent class to "class Animal(object)" but did not seem to work. – Hanshenry90 Apr 10 '17 at 00:16
  • What is the error message ? Is the indentation correct ? – t.m.adam Apr 10 '17 at 00:38
  • I do not think it is indent problem. It says: return "{} is {} cm tall and {} kilograms and says{} its owner is {}".format(self.__name, AttributeError: 'Dog' object has no attribute '_Dog__name' – Hanshenry90 Apr 10 '17 at 01:21
  • It seems after I added __metaclass__ = type at the beginning, the problem is solved. – Hanshenry90 Apr 11 '17 at 22:32
1

After I added metaclass = type at the beginning, the problem is solved. I am not quite sure but seems the metaclass allows python2.7 to access python3's library.

Hanshenry90
  • 109
  • 1
  • 8