Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
super() in class
#1
hi
in code:
# from: https://roocket.ir/discuss/%D8%B9%D9%85%D9%84%DA%A9%D8%B1%D8%AF-
# super-%D8%AF%D8%B1-%D8%A7%D8%B1%D8%AB-%D8%A8%D8%B1%DB%8C-%D8%A7%D8%B2-
# %DA%86%D9%86%D8%AF-%DA%A9%D9%84%D8%A7%D8%B3-%D8%AF%D8%B1-
# %D9%BE%D8%A7%DB%8C%D8%AA%D9%88%D9%86

# usage of super()

class First():
    def __init__(self):
        print ("first")
 
class Second(First):
    def __init__(self):
        super().__init__()
        print ("second")
 
class Third(First):
    def __init__(self):
        super()
        print ("third")
 
class Fourth(Second ,Third):
    def __init__(self):
        super().__init__()
        print ("that's it")
 
Fourth()
# output is:
"""
third
second
that's it
"""

class Third2(First):
    def __init__(self):
        super().__init__()
        print ("third")


class Fourth2(Second ,Third2):
    def __init__(self):
        super().__init__()
        print ("that's it")
 
Fourth2()
# output is:
"""
first  
third  
second  
that's it
"""
as you can see in Second class(line 14) there is super().__init__ and in Third class(line 19) there is super().
what is super() and what is it used for?
what is the difference between the two above-mentioned lines?
I searched and read some on the net for super() and in Python documentation but I did not get anything of them and I did not get a suitable explanation about super(). plz, explain about it.
in some codes I saw such as super().c(). i think c() is a method.( code is long, so I did not import it here)
what is it? again, explanation
thanks
Reply
#2
From the docs

Quote:There are two typical use cases for super. In a class hierarchy with single inheritance, super can be used to refer to parent classes without naming them explicitly, thus making the code more maintainable. This use closely parallels the use of super in other programming languages.

The second use case is to support cooperative multiple inheritance in a dynamic execution environment. This use case is unique to Python and is not found in statically compiled languages or languages that only support single inheritance. This makes it possible to implement “diamond diagrams” where multiple base classes implement the same method. Good design dictates that this method have the same calling signature in every case (because the order of calls is determined at runtime, because that order adapts to changes in the class hierarchy, and because that order can include sibling classes that are unknown prior to runtime).
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problems with super() Hoespilaar 2 271 Apr-10-2024, 10:10 AM
Last Post: Hoespilaar
  super() and order of running method in class inheritance akbarza 7 765 Feb-04-2024, 09:35 AM
Last Post: Gribouillis
  superclass and super() grkiran2011 1 1,750 Jun-20-2020, 04:37 AM
Last Post: deanhystad
  MRO About super() catlessness 1 2,055 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,474 May-04-2019, 08:30 PM
Last Post: Shafla
  Is any super keyword like java rajeev1729 2 3,317 Sep-14-2017, 07:47 PM
Last Post: snippsat
  Multiple Inheritance using super() Sagar 2 7,313 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