which two properties of the context are used to derive the two arguments for super when it is called without any?
more generally, given any python code where the statement super()
is executed, how do i determine the 2 arguments it is implicitly called with.
if this understanding of the argumentless super() call is incorrect, elaborate how it is handled instead
many thanks for your answer
The docstring states super() -> same as super(__class__, <first argument>)
. When a function is compiled as a method in a class body, using super()
without arguments implicitly defines a __class__
closure that references the defining class. The __class__
closure can also be referenced explicitly by a method. For example:
class A:
def m1(self):
super()
def m2(self):
__class__
>>> A.m1.__code__.co_freevars
('__class__',)
>>> A.m2.__code__.co_freevars
('__class__',)
>>> A.m1.__closure__[0].cell_contents is A
True
>>> A.m2.__closure__[0].cell_contents is A
True
oh wow, i even remember looking at help(super)
, i have no clue how i overlooked that. thanks nonetheless
Reference Stack Overflow canonical:
the top answer describes the use of super
with no arguments (a 3.x-specific convenience, which depends on inspecting the stack behind the scenes).