3

Initially this was defined

class Mammal(object):
    def __init__(self, name):
        self.name = name

    def get_name(self):
        return self.name

    def say(self):
        return("What does the " + self.name + " says")

but now we want to create subclasses of Mammals, whose constructor will call the Mammal's constructor with the correct name.

  class Dog(Mammal):
        def __init__(self):
            Dog.self

This is my code. It says type object 'Dog' has no attribute 'self' what's the problem?

when print(Dog().get_name()) I should get Dog.

Selcuk
  • 57,004
  • 12
  • 102
  • 110
user3398505
  • 607
  • 1
  • 7
  • 13

2 Answers2

4

If you are using Python 2.x, you should write

super(Dog, self).__init__('name')

or, for Python 3:

super().__init__('name')

instead of

Dog.self

See Understanding Python super() with __init__() methods for detail.

If you want Dog().get_name() to return 'Dog', you should call

super(Dog, self).__init__('Dog')
Community
  • 1
  • 1
Selcuk
  • 57,004
  • 12
  • 102
  • 110
3

You should write like this:

class Dog(Mammal):
    def __init__(self):
        super().__init__('dog name')
Taha Jahangir
  • 4,774
  • 2
  • 42
  • 49