Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8b6b176

Browse files
committed
#16440: fix exception type and clarify example.
1 parent 5c90436 commit 8b6b176

1 file changed

Lines changed: 15 additions & 9 deletions

File tree

Doc/library/stdtypes.rst

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2659,16 +2659,22 @@ arg-n)``.
26592659
Like function objects, bound method objects support getting arbitrary
26602660
attributes. However, since method attributes are actually stored on the
26612661
underlying function object (``meth.__func__``), setting method attributes on
2662-
bound methods is disallowed. Attempting to set a method attribute results in a
2663-
:exc:`TypeError` being raised. In order to set a method attribute, you need to
2664-
explicitly set it on the underlying function object::
2662+
bound methods is disallowed. Attempting to set an attribute on a method
2663+
results in an :exc:`AttributeError` being raised. In order to set a method
2664+
attribute, you need to explicitly set it on the underlying function object::
26652665

2666-
class C:
2667-
def method(self):
2668-
pass
2669-
2670-
c = C()
2671-
c.method.__func__.whoami = 'my name is c'
2666+
>>> class C:
2667+
... def method(self):
2668+
... pass
2669+
...
2670+
>>> c = C()
2671+
>>> c.method.whoami = 'my name is method' # can't set on the method
2672+
Traceback (most recent call last):
2673+
File "<stdin>", line 1, in <module>
2674+
AttributeError: 'method' object has no attribute 'whoami'
2675+
>>> c.method.__func__.whoami = 'my name is method'
2676+
>>> c.method.whoami
2677+
'my name is method'
26722678

26732679
See :ref:`types` for more information.
26742680

0 commit comments

Comments
 (0)