Python Forum
super() and order of running method in class inheritance
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
super() and order of running method in class inheritance
#8
(Feb-04-2024, 06:28 AM)akbarza Wrote: how does Python recognize this argument for super() in that line?
It is a very good question. I don't know the complete answer however the documentation of super() gives this example (with this exact comment)
class C(B):
    def method(self, arg):
        super().method(arg)    # This does the same thing as:
                               # super(C, self).method(arg)
Furthermore, if you type help(super) in the Python console, it says
class super(object)
 |  super() -> same as super(__class__, <first argument>)
 |  super(type) -> unbound super object
 |  super(type, obj) -> bound super object; requires isinstance(obj, type)
 |  super(type, type2) -> bound super object; requires issubclass(type2, type)
...
this suggests that it uses a local hidden variable named __class__. This variable can indeed be accessed in a method
>>> class A:
...     def foo(self):
...         print(__class__)
... 
>>> A().foo()
<class '__main__.A'>
>>> 
Further investigation indicates that this variable is loaded from a place called 'fast locals storage'
>>> import dis
>>> dis.dis(A.foo)
  3           0 LOAD_GLOBAL              0 (print)
              2 LOAD_DEREF               0 (__class__)
              4 CALL_FUNCTION            1
              6 POP_TOP
              8 LOAD_CONST               0 (None)
             10 RETURN_VALUE
>>> 
Documentation Wrote:LOAD_DEREF(i)
Loads the cell contained in slot i of the “fast locals” storage. Pushes a reference to the object the cell contains on the stack.
akbarza likes this post
« We can solve any problem by introducing an extra level of indirection »
Reply


Messages In This Thread
RE: super() and order of running method in class inheritance - by Gribouillis - Feb-04-2024, 09:35 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Problems with super() Hoespilaar 2 393 Apr-10-2024, 10:10 AM
Last Post: Hoespilaar
  class definition and problem with a method HerrAyas 2 398 Apr-01-2024, 03:34 PM
Last Post: HerrAyas
  super() in class akbarza 1 567 Dec-19-2023, 12:55 PM
Last Post: menator01
  the order of running code in a decorator function akbarza 2 627 Nov-10-2023, 08:09 AM
Last Post: akbarza
  "Name is not defined" when running a class lil_e 6 4,492 Jan-12-2023, 11:57 PM
Last Post: lil_e
  Using one child class method in another child class garynewport 5 1,781 Jan-11-2023, 06:07 PM
Last Post: garynewport
  Child class inheritance issue eakanathan 3 1,471 Apr-21-2022, 12:03 PM
Last Post: deanhystad
  [Solved] Novice question to OOP: can a method of class A access attributes of class B BigMan 1 1,398 Mar-14-2022, 11:21 PM
Last Post: deanhystad
  class, attribute and method Frankduc 9 2,655 Feb-27-2022, 09:07 PM
Last Post: deanhystad
  Subclass initialized property used in parent class method. Is it bad coding practice? saavedra29 5 1,982 Feb-07-2022, 07:29 PM
Last Post: saavedra29

Forum Jump:

User Panel Messages

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