Help with super()

David Hirschfield davidh at ilm.com
Thu Jan 12 13:46:02 EST 2006


I'm having trouble with the new descriptor-based mechanisms like super() 
and property() stemming, most likely, from my lack of knowledge about 
how they work.

Here's an example that's giving me trouble, I know it won't work, but it 
illustrates what I want to do:

class A(object):
    _v = [1,2,3]
   
    def _getv(self):
        if self.__class__ == A:
            return self._v
        return super(self.__class__,self).v + self._v

    v = property(_getv)
   

class B(A):
    _v = [4,5,6]
   
b = B()
print b.v

What I want is for b.v to give me back [1,2,3,4,5,6], but this example 
gets into a recursive infinite loop, since super(B,self).v is still 
B._getv(), not A._getv().

Is there a way to get what I'm after using super()?

The idea is that I could have a chain of subclasses which only need to 
redefine _v, and getting the value of v as a property would give me back 
the full chain of _v values for that class and all its ancestor classes.

Thanks in advance,
-David

-- 
Presenting:
mediocre nebula.




More information about the Python-list mailing list