UPDATE: please see my comment below. I don't see how the other article answers the very specific question I am asking. The closest it comes is when it says if I have an unrelated variables named super, things will work. But that a) doesn't match my situation since I do not have an unrelated variable named super, and b) doesn't explain how an unrelated super variable would cause the code to succeed, anyway. I want to know about an unrelated reference to the normal (and therefore related) super, and I want to know why/how it works.
I was looking at Why is Python 3.x's super() magic? and decided to fiddle around with the code. As I was doing so, I found that the call super_().x()
in B.x()
(in the code below) succeeds if the expression super
is executed, but fails if it does not. At least some other references to super have the same effect - for example, I found the same results using z = super
or print(id(super))
.
Why does the call to B.x() succeed when this seemingly-meaningless reference to super
is included?
super_ = super
class A(object):
def x(self):
print("Hello, world.")
class B(A):
def x(self):
# if this evaluation is present, the call to super_() succeeds.
# if this evaluation is not present, the call to super_() fails.
print(id(super))
super_().x()
if __name__ == '__main__':
B().x()