Why Python 3.x's super() Can Perform Magic
In Python 3.x, the super() function can be invoked without arguments. This allows for simplified and improved superclass invocation, as demonstrated below:
<code class="python">class A(object): def x(self): print("Hey now") class B(A): def x(self): super().x() B().x() # Outputs: "Hey now"</code>
This functionality is achieved through compile-time magic, which ensures that super() has access to the correct superclass at runtime. However, this behavior can also lead to unexpected errors when super() is rebound to a different name, as illustrated below:
<code class="python">super_ = super class A(object): def x(self): print("No flipping") class B(A): def x(self): super_().x() B().x() # Raises: RuntimeError: super(): __class__ cell not found</code>
Unveiling the Inner Workings of super()
The magic behind super() lies in a compile-time class cell that is created when super or class is referenced within a method. This cell provides super() with access to the original class object, even if the class name has been rebound.
This class cell mechanism was introduced to prevent errors caused by explicitly naming the class when invoking super() or using class decorators that return new class objects. It also avoids the misapplication of super(), such as invocations using super(type(self), self) or super(self.__class__, self), which can lead to infinite recursion.
Practical Applications and Gotchas
While the class cell adds convenience, it can also lead to unexpected behavior in certain scenarios. For example, if super() is rebound to a different name (e.g., super_ as shown earlier) and the method does not reference class explicitly, the super() call will fail.
Another example where knowing about the underlying mechanism can be beneficial is when using class decorators. If a decorator returns a new class object, the class cell will continue to refer to the original class, ensuring correct superclass invocation.
It is worth noting that rebinding other functions, methods, or classes in Python can also lead to unexpected behavior. However, super() is a particularly notable example due to its central role in object-oriented programming.
The above is the detailed content of Why Does Python 3.x\'s `super()` Have This \'Magic\' Behavior, and When Can It Lead to Errors?. For more information, please follow other related articles on the PHP Chinese website!