homepage

This issue tracker has been migrated to GitHub , and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Inconsistent results with super and __getattribute__
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 2.7
process
Status: closed Resolution: duplicate
Dependencies: Superseder: super() and property inheritance behavior
View: 14965
Assigned To: Nosy List: BreamoreBoy, ajaksu2, grodr
Priority: normal Keywords:

Created on 2003年08月05日 14:19 by grodr, last changed 2022年04月10日 16:10 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
OP_example.py ajaksu2, 2009年02月13日 04:00 OP's example, cleaned up
Messages (3)
msg60365 - (view) Author: Gonçalo Rodrigues (grodr) Date: 2003年08月05日 14:19
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.
msg81870 - (view) Author: Daniel Diniz (ajaksu2) * (Python triager) Date: 2009年02月13日 04:00
Same behavior, is this a real issue?
Current exception:
TypeError: 'super' object has only read-only attributes (assign to .n)
msg114254 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2010年08月18日 17:27
Closed as no response to msg81870.
History
Date User Action Args
2022年04月10日 16:10:29adminsetgithub: 39013
2013年02月24日 01:08:24r.david.murraysetstatus: open -> closed
superseder: super() and property inheritance behavior
resolution: duplicate
stage: test needed -> resolved
2010年08月18日 17:27:32BreamoreBoysetnosy: + BreamoreBoy
messages: + msg114254
2009年02月13日 04:00:50ajaksu2setfiles: + OP_example.py
versions: + Python 2.7, - Python 2.3
nosy: + ajaksu2
messages: + msg81870
type: behavior
stage: test needed
2003年08月05日 14:19:12grodrcreate

AltStyle によって変換されたページ (->オリジナル) /