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

Skip to content

Commit 65ea9bd

Browse files
committed
Bug #1684991: explain __special__ lookup semantics.
1 parent 08898b4 commit 65ea9bd

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

Doc/reference/datamodel.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,6 +1060,20 @@ raise an exception when no appropriate method is defined.
10601060

10611061
.. XXX above translation is not correct for new-style classes!
10621062
1063+
Special methods are only guaranteed to work if defined in an object's class, not
1064+
in the object's instance dictionary. That explains why this won't work::
1065+
1066+
>>> class C:
1067+
... pass
1068+
...
1069+
>>> c = C()
1070+
>>> c.__len__ = lambda: 5
1071+
>>> len(c)
1072+
Traceback (most recent call last):
1073+
File "<stdin>", line 1, in <module>
1074+
TypeError: object of type 'C' has no len()
1075+
1076+
10631077
When implementing a class that emulates any built-in type, it is important that
10641078
the emulation only be implemented to the degree that it makes sense for the
10651079
object being modelled. For example, some sequences may work well with retrieval

0 commit comments

Comments
 (0)