[Python-checkins] r57993 - python/trunk/Doc/reference/datamodel.rst

georg.brandl python-checkins at python.org
Wed Sep 5 15:36:44 CEST 2007


Author: georg.brandl
Date: Wed Sep  5 15:36:44 2007
New Revision: 57993

Modified:
   python/trunk/Doc/reference/datamodel.rst
Log:
Backport from Py3k: Bug #1684991: explain lookup semantics for __special__ methods (new-style classes only).


Modified: python/trunk/Doc/reference/datamodel.rst
==============================================================================
--- python/trunk/Doc/reference/datamodel.rst	(original)
+++ python/trunk/Doc/reference/datamodel.rst	Wed Sep  5 15:36:44 2007
@@ -1147,6 +1147,21 @@
 ``x.__getitem__(i)``.  Except where mentioned, attempts to execute an operation
 raise an exception when no appropriate method is defined.
 
+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


More information about the Python-checkins mailing list