[Python-Dev] Super and properties (original) (raw)
Gon�alo Rodrigues op73418@mail.telepac.pt
Wed, 2 Apr 2003 15:42:41 +0100
- Previous message: [Python-Dev] python-dev Summary for 2003-03-16 through 2003-03-31
- Next message: [Python-Dev] Super and properties
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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 8 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 "", 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) 8
Or, not hardcoding the test class,
b.class.mro[1].n.get(b) 8
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
- Previous message: [Python-Dev] python-dev Summary for 2003-03-16 through 2003-03-31
- Next message: [Python-Dev] Super and properties
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]