diff options
author | Georg Brandl <georg@python.org> | 2007-09-05 13:36:27 (GMT) |
---|---|---|
committer | Georg Brandl <georg@python.org> | 2007-09-05 13:36:27 (GMT) |
commit | 65ea9bd87e294ec9eb80f2686bbb9d17276a14e4 (patch) | |
tree | 899c7ee9c96b721ba6f97cd70fb7028b287edb3e /Doc/reference | |
parent | 08898b4b19a0cf0a5707efc3e64c0d2e3cf3d82f (diff) | |
download | cpython-65ea9bd87e294ec9eb80f2686bbb9d17276a14e4.zip cpython-65ea9bd87e294ec9eb80f2686bbb9d17276a14e4.tar.gz cpython-65ea9bd87e294ec9eb80f2686bbb9d17276a14e4.tar.bz2 |
Bug #1684991: explain __special__ lookup semantics.
Diffstat (limited to 'Doc/reference')
-rw-r--r-- | Doc/reference/datamodel.rst | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/Doc/reference/datamodel.rst b/Doc/reference/datamodel.rst index 9e847cc..29e6220 100644 --- a/Doc/reference/datamodel.rst +++ b/Doc/reference/datamodel.rst @@ -1060,6 +1060,20 @@ raise an exception when no appropriate method is defined. .. XXX above translation is not correct for new-style classes! +Special methods are only guaranteed to work if defined in an object's class, not +in the object's instance dictionary. That explains why this won't work:: + + >>> class C: + ... pass + ... + >>> c = C() + >>> c.__len__ = lambda: 5 + >>> len(c) + Traceback (most recent call last): + File "<stdin>", line 1, in <module> + TypeError: object of type 'C' has no len() + + When implementing a class that emulates any built-in type, it is important that the emulation only be implemented to the degree that it makes sense for the object being modelled. For example, some sequences may work well with retrieval |