Python Forum
Basic Inheritance, Why Use init
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Basic Inheritance, Why Use init
#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


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,140 Sep-19-2022, 02:32 AM
Last Post: Xeno
  Init an indefinite number of class MathisDELAGE 9 2,486 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,404 May-27-2020, 10:00 AM
Last Post: snippsat
  Is it mandatory to call superclass init inside the class init? psolar 3 6,268 Feb-14-2020, 09:16 PM
Last Post: wavic
  init vs_init_ while defining method/function? hsunteik 1 3,712 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