Aug-11-2018, 10:23 AM
Can just use
There is
So
self.eat()
insted of Animal.eat(self)
.There is
super()
which is more modern an better way.class Animal: hello = 23 def __init__(self): print("Animal created") def whoAmI(self): print("Animal") def eat(self): print("Eating") class Dog(Animal): def __init__(self): super().__init__() print("Dog created") def whoAmI(self): print("Dog") def bark(self): super().eat() print(Animal.hello) print("Woof!") my_dog = Dog() my_dog.bark()
Output:Animal created
Dog created
Eating
23
Woof!
Here super()
used both for __init__
and method Inheritance.So
my_dog
object has inherited eat()
from __init__
then you want to also call eat()
in method bark.>>> # __init__ >>> my_dog.eat() Eating >>> # super call of method eat() in parent class >>> my_dog.bark() Eating # super 23 Woof!