Help with super()

David Hirschfield davidh at ilm.com
Thu Jan 12 19:22:17 EST 2006


I tried that and super(B,self), but neither works.

Using super(A,self) in the _getv(self) method doesn't work, since the 
super() of A is "object" and that doesn't have the v property at all.
Not sure why you say that using self.__class__ is wrong as the first 
argument to super(), it should be the same as using the class name 
itself - both will result in <class blah.B> or whetever self is an 
instance of.

I still don't see a way to accomplish my original goal, but any other 
suggestions you might have would be appreciated.
Thanks,
-David

Mike Meyer wrote:

>David Hirschfield <davidh at ilm.com> writes:
>  
>
>>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()?
>>    
>>
>
>Yes. Call super with A as the first argument, not self.__class__.
>
>That's twice in the last little bit I've seen someone incorrectly use
>self.__class__ instead of using the class name. Is there bogus
>documentation somewhere that's recommending this?
>
>          <mike
>  
>

-- 
Presenting:
mediocre nebula.




More information about the Python-list mailing list