Help with super()

Paul McNett p at ulmcnett.com
Thu Jan 12 19:52:02 EST 2006


David Hirschfield wrote:
> 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.

Your basic problem is that property fset(), fget() and friends are defined at 
the class level, not at the instance level. So if you want to override the 
setter of a property in a subclass, you pretty much have to redefine the 
property in that subclass. And therefore, you also have to redefine the getter 
of the property as well. There is no easy way to "subclass" property getters and 
setters.

But, there's another problem with your example code as well. You appear to 
assume that self._v is going to refer to the _v defined in that class. But take 
a look at this:

class A(object):
   _v = [1,2,3]

   def _getv(self):
     print self._v ## hey, look, I'm [4,5,6]!!!
   v = property(_getv)


class B(A):
   _v = [4,5,6]

b = B()

print b.v


-- 
Paul McNett
http://paulmcnett.com
http://dabodev.com




More information about the Python-list mailing list