Attribute value of instance method non-modifiable?

S

Stefan Behnel

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']


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,149
Members
46,695
Latest member
StanleyDri

Latest Threads

Top