[Python-Dev] Super and properties

Gonšalo Rodrigues op73418@mail.telepac.pt
Wed, 2 Apr 2003 15:42:41 +0100

Hi all,

Since this is my first post here, let me first introduce myself. I'm Gonšalo
Rodrigues. I work in mathematics, mathematical physics to be more precise. I
am a self-taught hobbyist programmer and fell in love with Python a year and
half ago. And of interesting personal details this is about all so let me
get down to business.

My problem has to do with super that does not seem to work well with
properties. I posted to comp.lang.python a while ago and there I was advised
to post here. So, suppose I override a property in a subclass,  e.g.

>>> class test(object):
...  def __init__(self, n):
...   self.__n = n
...  def __get_n(self):
...   return self.__n
...  def __set_n(self, n):
...   self.__n = n
...  n = property(__get_n, __set_n)
>>> a = test(8)
>>> a.n
>>> class test2(test):
...  def __init__(self, n):
...   super(test2, self).__init__(n)
...  def __get_n(self):
...   return "Got ya!"
...  n = property(__get_n)
>>> b = test2(8)
>>> b.n
'Got ya!'

Now, since I'm overriding a property, it is only normal that I may want to
call the property implementation in the super class. But the obvious way (to
me at least) does not work:

>>> print super(test2, b).n
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
AttributeError: 'super' object has no attribute 'n'

I know I can get at the property via the class, e.g. do

>>> test.n.__get__(b)

Or, not hardcoding the test class,

>>> b.__class__.__mro__[1].n.__get__(b)

But this is ugly at best. To add to the puzzle, the following works, albeit
not in the way I expected

>>> super(test2, b).__getattribute__('n')
'Got ya!'

Since I do not know if this is a bug in super or a feature request for it, I
thought I'd better post here and leave it to your consideration.

With my best regards,
G. Rodrigues