Improved super/autosuper

Delaney, Timothy C (Timothy) tdelaney at avaya.com
Tue Jul 6 02:12:33 CEST 2004


OK - I've updated the recipe (though it's not available for general
viewing yet).
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/286195

autosuper is now a mixin class. Two properties are available -
self.super and self.super_raise. Use self.super if you don't want an
AttributeError to be raised if no base class has the attribute. Use
self.super_raise if you do.

The object returned by self.super and self.super_raise is a callable
wrapper. If you call it, it will call the method on a base class with
the same name as the method that self.super was called from.
Alternately, you can access attributes/methods on the returned object.

    class A (autosuper):

        def __init__ (self, a, b):
            print 'A.__init__'
            print a, b
            self.super(a, b)

        def test (self, a, b):
            print 'A.test'
            print a, b
            self.super(a, b)

    class B (A):

        def __init__ (self):
            print 'B.__init__'
            self.super(1, 2)
            self.super.test(3, 4)

    B()

produces:

    B.__init__
    A.__init__
    1 2
    A.test
    3 4

Code is attached.

Tim Delaney
-------------- next part --------------
A non-text attachment was scrubbed...
Name: super.py
Type: application/octet-stream
Size: 7424 bytes
Desc: super.py
URL: <http://mail.python.org/pipermail/python-list/attachments/20040706/9231322b/attachment.obj>


More information about the Python-list mailing list