Feb-04-2024, 09:35 AM
(This post was last modified: Feb-04-2024, 09:38 AM by Gribouillis.)
(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 saysclass 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.
« We can solve any problem by introducing an extra level of indirection »