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: Attempting to override special methods of a function object does not cause an error
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 2.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Chinmay.Kanchi, eric.araujo, r.david.murray
Priority: normal Keywords:

Created on 2010-12-08 01:30 by Chinmay.Kanchi, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (4)
msg123589 - (view) Author: Chinmay Kanchi (Chinmay.Kanchi) Date: 2010-12-08 01:30
Attempting to override a special method of an object of a builtin (like list) raises an AttributeError. This is obviously by design. However, doing the same to a user-defined function object seemingly replaces the function, but does not have the expected effect. In the interests of consistency, attempting to change a special method of a function object should raise an AttributeError stating that the property/method is read-only.

>>> a_list = list()
>>> a_list.__repr__ = lambda: '[]'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'list' object attribute '__repr__' is read-only

>>> def f(): pass
>>> f.__repr__ = lambda: 'f'
>>> f.__repr__
<function <lambda> at 0x6482b0>
>>> repr(f) #would expect it to return 'f' since no error was raised
'<function f at 0x6481f0>'
>>> f.__repr__() #so the change is half-way made, inconsistent and possibly problematic
'f'
>>>
msg123591 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2010-12-08 02:29
As you observe, the attribute is not read only, it simply isn't referred to when special method lookup is done.  This is specified as part of the language design for new style classes, but has only been made consistently true in recent versions.

On on the other hand, the special methods are not special in regard to their read-only nature on builtin types:

>>> l = list()
>>> l.append = '1'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'list' object attribute 'append' is read-only

Thus, this is not a bug, it's just the way the language works.
msg123611 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2010-12-08 15:00
More info:
http://docs.python.org/dev/reference/datamodel#special-method-names
http://docs.python.org/dev/reference/datamodel#special-method-lookup-for-new-style-classes
msg123613 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2010-12-08 15:02
Oops, that second links has been renamed:
http://docs.python.org/dev/reference/datamodel#special-method-lookup
History
Date User Action Args
2022-04-11 14:57:09adminsetgithub: 54858
2010-12-08 15:02:58eric.araujosetmessages: + msg123613
2010-12-08 15:00:23eric.araujosetnosy: + eric.araujo
messages: + msg123611
2010-12-08 02:29:44r.david.murraysetstatus: open -> closed

nosy: + r.david.murray
messages: + msg123591

resolution: not a bug
stage: resolved
2010-12-08 01:30:33Chinmay.Kanchicreate