
After the initial controversy regarding Python's super(), I feel that it's now fairly well-understood when it should be used and what problems it solves. (Even if not, this list is probably not the right medium for objections to super.) The one question I haven't seen covered is whether and how to use it from C.
All of the Python/C code I've seen calls its base class(es) directly, typically by only invoking the method of their superclass in the C layout sense. This means that such code will call into the superclass twice in a diamond inheritance scenario. For low-level classes that are not expected to be multiply inherited this is not a problem. But in some cases we need to convert higher-level classes from Python to C for efficiency, either using Python/C, or a higher-level C++ wrapping. The classes use super() with good reason, and I'd like to avoid breaking them during conversion to C.
Is there a documented way to use super() from C, other than instantiating PySuper_Type? Has anyone tried to do that?

On 2009-03-11 10:22, Hrvoje Niksic wrote:
After the initial controversy regarding Python's super(), I feel that it's now fairly well-understood when it should be used and what problems it solves. (Even if not, this list is probably not the right medium for objections to super.) The one question I haven't seen covered is whether and how to use it from C.
All of the Python/C code I've seen calls its base class(es) directly, typically by only invoking the method of their superclass in the C layout sense. This means that such code will call into the superclass twice in a diamond inheritance scenario. For low-level classes that are not expected to be multiply inherited this is not a problem. But in some cases we need to convert higher-level classes from Python to C for efficiency, either using Python/C, or a higher-level C++ wrapping. The classes use super() with good reason, and I'd like to avoid breaking them during conversion to C.
Is there a documented way to use super() from C, other than instantiating PySuper_Type? Has anyone tried to do that?
If you look at the C code for the builtins module, you'll find that super() is just an alias for the PySuper_Type, so instantiating this corresponds 1-1 to the Python logic.
The complicated lookup code for the base method is not exposed in the typeobject.c file as C API, so there's no other way to access it.
Perhaps there should be... most uses of super() will not cache the super-object and only use it once, so apart from the syntactic sugar added by having super() return the super-lookup-object in order to be able to reference the method in a more convenient way, there doesn't appear to be much reason for creating a new object just to call the base method.
participants (2)
-
Hrvoje Niksic
-
M.-A. Lemburg