Python Forum
Is any super keyword like java
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Is any super keyword like java
#1
how to print "Calling parent method" using Child object.
class Parent:        # define parent class
   def myMethod(self):
      print ('Calling parent method')

class Child(Parent): # define child class
   def myMethod(self):
      
      print ('Calling child method')

c = Child()          # instance of child
c.myMethod()         # child calls overridden method
Reply
#2
https://www.blog.pythonlibrary.org/2014/...-is-super/
Reply
#3
The Super() call has been simpler in Python 3,in link of buran is mention but not shown.
So super(Child, self).my_method() becomes super().my_method().
λ ptpython
>>> class Parent:
...     def my_method(self):
...         print('Calling parent method')
...
... class Child(Parent):
...     def my_method(self):
...         print('Calling child method')
...         super().my_method()

>>> c = Child()
>>> c.my_method()
Calling child method
Calling parent method
You must always call the original implementation.
By calling the original implementation,you get the result you later want to improve.
Super() follow mro that moving up the inheritance tree.
>>> Child.mro()
[<class '__main__.Child'>, <class '__main__.Parent'>, <class 'object'>]
When override have to think if you want to filter the arguments for the original implementation,
before,after or both.
Example pre-filter.
Add some information before calling(original implementation) of time_show method.
λ ptpython
>>> from datetime import datetime
...
... class Show(object):
...     def time_show(self, message):
...         print(message)
...
... class TimeNow(Show):
...     def time_show(self, message):
...         message = f"<{datetime.now()}> <{message}>"
...         super().time_show(message)

>>> t = TimeNow()
>>> t.time_show('Hello world')
<2017-09-14 21:42:45.814203> <Hello world>
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problems with super() Hoespilaar 2 302 Apr-10-2024, 10:10 AM
Last Post: Hoespilaar
  super() in class akbarza 1 502 Dec-19-2023, 12:55 PM
Last Post: menator01
  Find a specific keyword after another keyword and change the output sgtmcc 5 886 Oct-05-2023, 07:41 PM
Last Post: deanhystad
  issue with Tabula-py, pyinstaller and java maurom82 2 3,225 Feb-19-2021, 04:32 PM
Last Post: buran
  superclass and super() grkiran2011 1 1,769 Jun-20-2020, 04:37 AM
Last Post: deanhystad
  MRO About super() catlessness 1 2,074 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,513 May-04-2019, 08:30 PM
Last Post: Shafla
  How to run python code in Java using remote SSH without py file using streams in Java varanasipavankumar 0 2,561 Apr-07-2019, 06:13 PM
Last Post: varanasipavankumar
  Jython code throws ImportError when invoked from a Java jar dchucks 6 7,188 Aug-02-2018, 05:09 AM
Last Post: dchucks
  phython language java integration jammytcs123123 1 2,319 Jul-04-2018, 03:13 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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