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
#2
A subclass needs __init__ if it needs to initialize attributes that are not inherited from the base class or needs to initialize existing attributes differently.
class Animal():
    varA = "---"
    def __init__(self, name, age, sound='...'):
        self.name = name
        self.age = age
        self.sound = sound
        print(self.name + " has been created")

    def talk(self):
        print(self.name, "Say :", self.sound)

    def myinfo(self):
        print(self.varA + " Info name: " +  self.name + " | Info age: " + str(self.age) + " " + self.varA)
 
class Cat(Animal):
    def __init__(self, name, age):
        super().__init__(name, age, 'Meow')

class Bird(Animal):
    def __init__(self, name, age):
        super().__init__(name, age, 'BrrBrr')
 
Reply
#3
it seems that you have modified your answer multiple times. Appreciated for your effort.
I'm still little confuse, but for now I will use __Init__ for subclass, at least force me make that into habit,
until next time I found "real" case that utilize max for using init and super. and can full understanding to use it

Thanks for your time and effort.
Reply
#4
The example you provide is not a very good example. There is no reason to subclass Animal because none of the animal subclasses do anything different. That is why you are having a hard time seeing why the subclasses should have an __init__.

Subclassing makes more sense when the subclasses add to the abilities they inherit from the superclass. In a GUI you have a Widget which is something that can be drawn in a Window. It can tell you how big it is and it should be and it can be added to a layout, but it doesn't do much else. It isn't very useful by itself. A Label is a Widget that draws text and optionally displays an image in a window. A Button is like a label, but it has a box drawn around the label and responds to button clicks. Each of these build on what they inherit and add new capabilities.

A Widget does not have text or images to draw, so it does not have text or image attributes. A Label has text and maybe an image, so the Label class needs an __init__ method so it can add text and image attributes to what is provided by Widget. Buttons call a function when pressed. A Label doesn't do this, so it would not have a callback attribute. Button needs an __init__ method so it can add the callback.

Don't write __init__ methods if you don't have to. Soon you will encounter cases where you need to, and then it will all make sense.
Reply
#5
The other part of the question, regarding super, is complex. Super in Python is different from super in most other languages and was instituted for cooperative multiple inheritance. Intrigued? Here is a Youtube of PyCon 2015 that covers it in some detail.
Super is Super
Reply
#6
That is just mean.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  PyRun_SimpleFile calling multiprocessing Python Class cause endless init loop Xeno 2 988 Sep-19-2022, 02:32 AM
Last Post: Xeno
  Init an indefinite number of class MathisDELAGE 9 2,226 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,174 May-27-2020, 10:00 AM
Last Post: snippsat
  Is it mandatory to call superclass init inside the class init? psolar 3 5,917 Feb-14-2020, 09:16 PM
Last Post: wavic
  init vs_init_ while defining method/function? hsunteik 1 3,614 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