[New-bugs-announce] [issue10649] Attempting to override special methods of a function object does not cause an error
report at bugs.python.org
Wed Dec 8 02:30:35 CET 2010
New submission from Chinmay Kanchi <cgkanchi at gmail.com>:
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'
<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
components: Interpreter Core
title: Attempting to override special methods of a function object does not cause an error
versions: Python 2.6
Python tracker <report at bugs.python.org>
More information about the New-bugs-announce