Help with super()
mwm at mired.org
Thu Jan 12 19:13:19 EST 2006
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 Meyer <mwm at mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
More information about the Python-list