[capi-sig] super() in Python/C

M.-A. Lemburg mal at egenix.com
Wed Mar 11 11:42:51 CET 2009

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

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.

Marc-Andre Lemburg

Professional Python Services directly from the Source  (#1, Mar 11 2009)
>>> Python/Zope Consulting and Support ...        http://www.egenix.com/
>>> mxODBC.Zope.Database.Adapter ...             http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ...        http://python.egenix.com/

::: Try our new mxODBC.Connect Python Database Interface for free ! ::::

   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
    D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
           Registered at Amtsgericht Duesseldorf: HRB 46611

More information about the capi-sig mailing list