Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Inheritance
#6
Can just use 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!
Reply


Messages In This Thread
Inheritance - by Athul - Aug-09-2018, 08:03 AM
RE: Inheritance - by ichabod801 - Aug-09-2018, 12:54 PM
RE: Inheritance - by micseydel - Aug-09-2018, 06:24 PM
RE: Inheritance - by Athul - Aug-11-2018, 07:35 AM
RE: Inheritance - by yksingh1097 - Aug-11-2018, 09:37 AM
RE: Inheritance - by snippsat - Aug-11-2018, 10:23 AM
RE: Inheritance - by ichabod801 - Aug-11-2018, 12:47 PM
RE: Inheritance - by yksingh1097 - Aug-11-2018, 06:48 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020