Next / Previous / Contents / Shipman's homepage

21.21. super(): Superclass

The purpose of this function is to retrieve the superclass of a given type or object. The general form is:

    super(C, i)
C

The class whose superclass is desired.

i

An instance of class C.

For example, suppose you wish to implement a class named OnceDict that works like the built-in dict class, except that it raises KeyError if you overwrite an existing member. Here is a complete implementation of that class:

class OnceDict(dict):
    '''Like a dict but refuses to allow redefinition.
    '''
    def __setitem__(self, key, value):
        if key in self:
            raise KeyError("Redefinition of {0!r} as "
                "{1!r} failed, already defined.".format(key, value))
        super(OnceDict, self).__setitem__(key, value)

The last line effectively calls dict.__setitem__().