Attribute value of instance method non-modifiable?

Discussion in 'Python' started by Stefan Behnel, Oct 26, 2005.

  1. Hi!

    I just stumbled over this:

    ..>>> class test(object):
    .... def t(): pass
    .... t.testval = 1
    ..>>> test.t
    <unbound method test.t>
    ..>>> test.t.testval
    ..>>> 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 Behnel, Oct 26, 2005
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.