[issue10649] Attempting to override special methods of a function object does not cause an error

Chinmay Kanchi 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'
>>> 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'
>>>

----------
components: Interpreter Core
messages: 123589
nosy: Chinmay.Kanchi
priority: normal
severity: normal
status: open
title: Attempting to override special methods of a function object does not cause an error
type: behavior
versions: Python 2.6

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue10649>
_______________________________________


More information about the Python-bugs-list mailing list