super. could there be a simpler super?

Gerrit Holl gerrit at
Thu Jan 15 15:22:48 CET 2004

Kristian Ovaska wrote:
> Kerim Borchaev <warkid at>:
> >  Is it possible that such a "super"(deducing class method declaration
> >  context) could appear in Python?
> >  (It seems to me that to implement a simple super something should be
> >  done during "compilation" of class declaration.)
> The document "Unifying types and classes in Python 2.2" by Guido
> probably answers your question.
> Quote from
> regarding super:
> "It would be nice if we didn't have to name the class explicitly, but
> this would require more help from Python's parser than we can
> currently get. I hope to fix this in a future Python release by making
> the parser recognize super."

Another quote from the same page :) :

--- start quote ---
Our second example creates a class, 'autosuper', which will add a
private class variable named __super, set to the value super(cls).
(Recall the discussion of self.__super above.) Now, __super is a private
name (starts with double underscore) but we want it to be a private name
of the class to be created, not a private name of autosuper. Thus, we
must do the name mangling ourselves, and use setattr() to set the class
variable. For the purpose of this example, I'm simplifying the name
mangling to "prepend an underscore and the class name". Again, it's
sufficient to override __init__ to do what we want, and again, we call
the base class __init__ cooperatively.

class autosuper(type):
    def __init__(cls, name, bases, dict):
        super(autosuper, cls).__init__(name, bases, dict)
        setattr(cls, "_%s__super" % name, super(cls))

Now let's test autosuper with the classic diamond diagram:

class A:
    __metaclass__ = autosuper
    def meth(self):
        return "A"
class B(A):
    def meth(self):
        return "B" + self.__super.meth()
class C(A):
    def meth(self):
        return "C" + self.__super.meth()
class D(C, B):
    def meth(self):
        return "D" + self.__super.meth()

assert D().meth() == "DCBA"

(Our autosuper metaclass is easily fooled if you define a subclass with
the same name as a base class; it should really check for that condition
and raise an error if it occurs. But that's more code than feels right
for an example, so I'll leave it as an exercise for the reader.) 

--- end quote ---

164. If his father-in-law do not pay back to him the amount of the
"purchase price" he may subtract the amount of the "Purchase price" from
the dowry, and then pay the remainder to her father's house.
          -- 1780 BC, Hammurabi, Code of Law
PrePEP: Builtin path type
Asperger's Syndrome - a personal approach:

More information about the Python-list mailing list