can someone explain 'super' to me?

Michael michaelmossey at yahoo.com
Sat Dec 5 05:27:54 EST 2009


>From the docs about the built-in function super:

----------------------------
super( type[, object-or-type])

Return the superclass of type. If the second argument is omitted the
super object returned is unbound. If the second argument is an object,
isinstance(obj, type) must be true. If the second argument is a type,
issubclass(type2, type) must be true. super() only works for new-style
classes.
A typical use for calling a cooperative superclass method is:

class C(B):
    def meth(self, arg):
        super(C, self).meth(arg)

Note that super is implemented as part of the binding process for
explicit dotted attribute lookups such as "super(C, self).__getitem__
(name)". Accordingly, super is undefined for implicit lookups using
statements or operators such as "super(C, self)[name]". New in version
2.2.
--------------------------------

It seems like it can return either a class or an instance of a class.
Like
super( C, self)
is like casting self as superclass C.
However if you omit the second argument entirely you get a class.

The former is considered a "bound" object. I'm really not clear on the
idea of "binding" in Python.

any pointers appreciated.
-Mike






More information about the Python-list mailing list