Message60365
Consider the following scenario of property overriding:
>>> class Test(object):
... def __init__(self, n):
... self.__n = n
... #Properties.
... def __get_n(self):
... return self.__n
... def __set_n(self, n):
... self.__n = n
... n = property(__get_n, __set_n)
...
>>> a = Test(42)
>>> a.n
42
>>> a.n = 32
>>> a.n
32
Now, let us override the n property:
>>> class Test2(Test):
... def __init__(self, n):
... super(Test2, self).__init__(n)
... #Properties.
... def __get_n(self):
... return "got ya!"
... def __set_n(self, value):
... print "No way, jose!"
... n = property(__get_n, __set_n)
...
>>> a = Test2(42)
>>> a.n
'got ya!'
>>> a.n = 0
No way, jose!
>>> a.n
'got ya!'
>>> super(Test2, a).n
42
So far so good, super is working well with properties. But
now consider the following inconsistencies:
>>> super(Test2, a).__getattribute__('n')
'got ya!'
>>> super(Test2, a).n
42
Also:
>>> super(Test, Test2).__getattribute__('n')
<property object at 0x01103300>
>>> super(Test, Test2).__getattribute__('n').__get__(a)
'got ya!'
>>> super(Test, Test2).n
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
AttributeError: 'super' object has no attribute 'n'
The last one is particularly damaging, because:
>>> super(Test2, a).n = 32
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
AttributeError: 'super' object has no attribute 'n'
I think the above should work, but whatever’s the
answer, together with the super(Test, Test2) odd
results it does make impossible using super and setting
properties together.
Tested with the latest Python 2.3 in win2k.
With my best regards,
G. Rodrigues
P.S: bug 729913 talks of similar issues at the end: super
does not seem to intercept special methods.
|
|
Date |
User |
Action |
Args |
2008-01-20 09:56:16 | admin | link | issue783528 messages |
2008-01-20 09:56:16 | admin | create | |
|