mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
#16440: fix exception type and clarify example.
This commit is contained in:
parent
5c90436d64
commit
8b6b176b33
1 changed files with 15 additions and 9 deletions
|
@ -2659,16 +2659,22 @@ arg-n)``.
|
||||||
Like function objects, bound method objects support getting arbitrary
|
Like function objects, bound method objects support getting arbitrary
|
||||||
attributes. However, since method attributes are actually stored on the
|
attributes. However, since method attributes are actually stored on the
|
||||||
underlying function object (``meth.__func__``), setting method attributes on
|
underlying function object (``meth.__func__``), setting method attributes on
|
||||||
bound methods is disallowed. Attempting to set a method attribute results in a
|
bound methods is disallowed. Attempting to set an attribute on a method
|
||||||
:exc:`TypeError` being raised. In order to set a method attribute, you need to
|
results in an :exc:`AttributeError` being raised. In order to set a method
|
||||||
explicitly set it on the underlying function object::
|
attribute, you 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 = C()
|
||||||
c.method.__func__.whoami = 'my name is 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: 'method' object has no attribute 'whoami'
|
||||||
|
>>> c.method.__func__.whoami = 'my name is method'
|
||||||
|
>>> c.method.whoami
|
||||||
|
'my name is method'
|
||||||
|
|
||||||
See :ref:`types` for more information.
|
See :ref:`types` for more information.
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue