How to call the super implementation of a property?

Beni Cherniavsky cben at techunix.technion.ac.il
Wed Jan 22 14:53:58 CET 2003


On 2003-01-21, Gonçalo Rodrigues wrote:

> I know I can do
>
> >>> test.n.__get__(b)
> 8
> >>>
>
> But how do I achieve the same using super (for an MI scenario)?
>
Seems that this part is lacking in `super` indeed (I played with it at the
prompt a long time with no success)...

If you control all your classes, there is a simple solution - use `super`
on the accessor method and not on the property:

class test(object):
    def get_n(self):
        return 1
    n = property(get_n)

class test2(test):
    def get_n(self):
        return super(test2, self).get_n() + 1
    n = property(get_n)

This only works if all the classes use the same name for the getter
function (that why I didn't use ``__get_n`` - why did you want mangling
anyway?).

The other solution is to implement your own lookup through the MRO - see
http://www.python.org/2.2.1/descrintro.html#cooperation for an example of
how the current behaiviour of `super` can be written in pure Python.

-- 
Beni Cherniavsky <cben at tx.technion.ac.il>





More information about the Python-list mailing list