super confused

Greg Chapman glc at well.com
Fri Sep 12 11:03:40 EDT 2003


On Tue, 09 Sep 2003 07:56:27 -0700, Daniel Klein <danielk at aracnet.com> wrote:

>In Smalltalk, a message send to 'super' simply means to send a message
>to self but start looking in the superclass first for the method. I
>realize Smalltalk is a single-inheritance language, but doesn't
>explain why, in Python, you need to specify the class name as a
>parameter.

You can get something like this using the autosuper trick from
Lib/test/test_descr.py.  You end up sending messages to self.__super, but at
least it avoids repeatedly typing long class names in the method
implementations.  If you don't like using metaclasses, you could create a
function to add the __super attribute after the class has been declared:

def class_mangle_name(klassname, attrname):
    klassname = re.sub('^_+', '', klassname)
    if klassname:
        return '_'+klassname+attrname
    return attrname


def addsuper(cls):
    setattr(cls, class_mangle_name(cls.__name__, '__super'), super(cls))

class ValidatedList(list):
    def append(self, objekt):
        _validate(objekt)
        self.__super.append(objekt)
    ...

addsuper(ValidatedList)

---
Greg Chapman
    




More information about the Python-list mailing list