Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
superclass and super()
#1
Hi Viewer,

I have two questions related to inheritance:

1. Let's say I have a class A and another class B which is inheriting A. So B is the subclass of A. When instantiating the class B does the super class A also get instantiated?

2. When I am using super() in the sub class what does it actually contain? The reference of the subclass or a reference to the super class? The self parameter contains the reference to the current class. Similarly does super() contain the reference to the super class?

Please clarify.

Thanks.
Reply
#2
1. No. You need to call the __init__ method for the super class from inside the __init__ method of the subclass. It is not called automatically.

2. In your example, calling super().__init__() from inside B.__init__ is the same as calling A.__init__(self). The argument for "self" is an instance of B, but remember that "B" is nothing but "A" with additional parts.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  super() in class akbarza 1 401 Dec-19-2023, 12:55 PM
Last Post: menator01
  Is it mandatory to call superclass init inside the class init? psolar 3 5,916 Feb-14-2020, 09:16 PM
Last Post: wavic
  MRO About super() catlessness 1 1,996 Jan-12-2020, 07:54 AM
Last Post: Gribouillis
  Super with Sublime Text - TypeError: super() takes at least 1 argument (0 given) Shafla 6 7,293 May-04-2019, 08:30 PM
Last Post: Shafla
  Mask superclass __init__ fig0 5 3,761 Jan-11-2018, 06:59 PM
Last Post: Gribouillis
  Is any super keyword like java rajeev1729 2 3,238 Sep-14-2017, 07:47 PM
Last Post: snippsat
  Multiple Inheritance using super() Sagar 2 7,244 Sep-08-2017, 08:58 AM
Last Post: Sagar

Forum Jump:

User Panel Messages

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