Commit 09017767 authored by Ezio Melotti's avatar Ezio Melotti

#16440: fix exception type and clarify example.

parent 3eb0e1da
...@@ -2878,16 +2878,23 @@ that class), otherwise a :exc:`TypeError` is raised. ...@@ -2878,16 +2878,23 @@ that class), otherwise a :exc:`TypeError` is raised.
Like function objects, methods objects support getting arbitrary attributes. Like function objects, methods objects support getting arbitrary attributes.
However, since method attributes are actually stored on the underlying function However, since method attributes are actually stored on the underlying function
object (``meth.im_func``), setting method attributes on either bound or unbound object (``meth.im_func``), setting method attributes on either bound or unbound
methods is disallowed. Attempting to set a method attribute results in a methods is disallowed. Attempting to set an attribute on a method results in
:exc:`TypeError` being raised. In order to set a method attribute, you need to an :exc:`AttributeError` being raised. In order to set a method attribute, you
explicitly set it on the underlying function object:: need to explicitly set it on the underlying function object::
class C: >>> class C:
def method(self): ... def method(self):
pass ... pass
...
>>> c = C()
>>> c.method.whoami = 'my name is method' # can't set on the method
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'instancemethod' object has no attribute 'whoami'
>>> c.method.im_func.whoami = 'my name is method'
>>> c.method.whoami
'my name is method'
c = C()
c.method.im_func.whoami = 'my name is c'
See :ref:`types` for more information. See :ref:`types` for more information.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment