Attribute value of instance method non-modifiable?

Stefan Behnel stefan.behnel-n05pAM at web.de
Wed Oct 26 14:44:20 EDT 2005


Hi!

I just stumbled over this:


.>>> class test(object):
...   def t(): pass
...   t.testval = 1
...
.>>> test.t
<unbound method test.t>
.>>> test.t.testval
1
.>>> test.t.testval = 2
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: 'instancemethod' object has no attribute 'testval'
.>>> dir(test.t)
['__call__', '__class__', '__cmp__', '__delattr__', '__doc__', '__get__',
'__getattribute__', '__hash__', '__init__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__setattr__', '__str__', 'im_class', 'im_func',
'im_self', 'testval']
>>> test.t.im_func.testval = 2
>>> test.t.testval
2
>>>



While I understand that a function is different from an instance method, it is
rather far from intuitive that the instance method provides read access to the
attribute of the enclosed function but not write access.

Does anyone know if this is a deliberate implementation decision? Is there any
reasoning behind that?

Stefan



More information about the Python-list mailing list