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


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