Python Forum
Basic Inheritance, Why Use init
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Basic Inheritance, Why Use init
#1
I'm new in Programing Language.

in here Bird and Cat are inheritance from Animal,
- Bird use __init__ and super
- Cat use nothing

but both have good result, inheritance from animal, the function work perfectly
is there explanation why we still use __init__ super since without it, inheritance still working and less code

Thanks

class Animal():
    varA = "---"
    def __init__(self,name,age):
        self.name = name
        self.age = age
        print(self.name + " has been created")
    def talk(self):
        print(self.name + "Say : ...")
    def myinfo(self):
        print(self.varA + " Info name: " +  self.name + " | Info age: " + str(self.age) + " " + self.varA)


class Cat(Animal):
    def talk(self):
        print(self.name + " Say: Meow")
 
 
class Bird(Animal):
    def __init__(self,name,age):
         super().__init__(name,age)     
    def talk(self):
        print(self.name + " Say: BrrrBrr")


print("------------------------------")
randomAnimal = Animal("randomAnimal",9)    
randomAnimal.talk()
randomAnimal.myinfo()
print("------------------------------")
blackCat = Cat("BlackCat",2)
blackCat.talk()
blackCat.myinfo()
print("------------------------------")
blueBird = Bird("BlueBird",3)
blueBird.talk()
blueBird.myinfo()
print("------------------------------")
Result Code:
Quote:------------------------------
randomAnimal has been created
randomAnimalSay : ...
--- Info name: randomAnimal | Info age: 9 ---
------------------------------
BlackCat has been created
BlackCat Say: Meow
--- Info name: BlackCat | Info age: 2 ---
------------------------------
blueBird has been created
blueBird Say: BrrrBrr
--- Info name: BlueBird | Info age: 3 ---
------------------------------
Reply


Messages In This Thread
Basic Inheritance, Why Use init - by udinjelek - Sep-28-2021, 02:59 AM
RE: Basic Inheritance, Why Use init - by deanhystad - Sep-28-2021, 11:55 AM
RE: Basic Inheritance, Why Use init - by udinjelek - Sep-29-2021, 04:41 AM
RE: Basic Inheritance, Why Use init - by deanhystad - Sep-29-2021, 04:21 PM
RE: Basic Inheritance, Why Use init - by jefsummers - Sep-29-2021, 05:36 PM
RE: Basic Inheritance, Why Use init - by deanhystad - Sep-29-2021, 06:03 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  PyRun_SimpleFile calling multiprocessing Python Class cause endless init loop Xeno 2 1,075 Sep-19-2022, 02:32 AM
Last Post: Xeno
  Init an indefinite number of class MathisDELAGE 9 2,391 Feb-18-2022, 07:49 PM
Last Post: deanhystad
  Error: How to to close and restart your shell after running 'conda init' angelica 3 10,322 May-27-2020, 10:00 AM
Last Post: snippsat
  Is it mandatory to call superclass init inside the class init? psolar 3 6,112 Feb-14-2020, 09:16 PM
Last Post: wavic
  init vs_init_ while defining method/function? hsunteik 1 3,674 Dec-24-2016, 08:27 AM
Last Post: micseydel

Forum Jump:

User Panel Messages

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