Author: georg.brandl Date: Wed Sep 5 15:36:27 2007 New Revision: 57992 Modified: python/branches/py3k/Doc/reference/datamodel.rst Log: Bug #1684991: explain __special__ lookup semantics.
Modified: python/branches/py3k/Doc/reference/datamodel.rst ============================================================================== --- python/branches/py3k/Doc/reference/datamodel.rst (original) +++ python/branches/py3k/Doc/reference/datamodel.rst Wed Sep 5 15:36:27 2007 @@ -1060,6 +1060,20 @@ .. 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 _______________________________________________ Python-3000-checkins mailing list [email protected] http://mail.python.org/mailman/listinfo/python-3000-checkins
