Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Inheritance
#1
|Hello,

Below is the example code from the tutorial that I'm using to learn python
class Animal:
    def __init__(self):
        print("Animal created")

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

    def eat(self):
        print("Eating")


class Dog(Animal):
    def __init__(self):
        Animal.__init__(self)
        print("Dog created")

    def whoAmI(self):
        print("Dog")

    def bark(self):
        print("Woof!")
I think I understand the concept of inheritance. but what confuses me is the use of Animal in Dog class

Animal.__init__(self)
Why here Animal used instead of self??
Is it to call the __init__ method of Dog class??
So if I want to use any method from say eat method from Animal class inside the bark method of derived the class, should I use Animal.eat(self). I tried self.eat(Self) but didn't work. ALso what is self inside the method call means in inheriatance

When to use self and base classes name when calling methods??


Then if there is a Class object attribute in Animal class say name = "Athul", and if want to access the name in derived class, should I use self.name or Animal.name??
How can I know which one to use??
What about in case of an attribute??

Thanks
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