[Python-checkins] r88149 - in python/branches/py3k: Doc/library/reprlib.rst Doc/whatsnew/3.2.rst Lib/reprlib.py

raymond.hettinger python-checkins at python.org
Sun Jan 23 22:05:46 CET 2011


Author: raymond.hettinger
Date: Sun Jan 23 22:05:46 2011
New Revision: 88149

Log:
Add entry for reprlib.

Modified:
   python/branches/py3k/Doc/library/reprlib.rst
   python/branches/py3k/Doc/whatsnew/3.2.rst
   python/branches/py3k/Lib/reprlib.py

Modified: python/branches/py3k/Doc/library/reprlib.rst
==============================================================================
--- python/branches/py3k/Doc/library/reprlib.rst	(original)
+++ python/branches/py3k/Doc/library/reprlib.rst	Sun Jan 23 22:05:46 2011
@@ -5,6 +5,9 @@
    :synopsis: Alternate repr() implementation with size limits.
 .. sectionauthor:: Fred L. Drake, Jr. <fdrake at acm.org>
 
+**Source code:** :source:`Lib/reprlib.py`
+
+--------------
 
 The :mod:`reprlib` module provides a means for producing object representations
 with limits on the size of the resulting strings. This is used in the Python

Modified: python/branches/py3k/Doc/whatsnew/3.2.rst
==============================================================================
--- python/branches/py3k/Doc/whatsnew/3.2.rst	(original)
+++ python/branches/py3k/Doc/whatsnew/3.2.rst	Sun Jan 23 22:05:46 2011
@@ -987,6 +987,32 @@
 
 (Patch submitted by Daniel Urban; :issue:`5867`.)
 
+reprlib
+-------
+
+When writing a :meth:`__repr__` method for a custom container, it is easy to
+forget to handle the case where a member refers back to the container itself.
+Python's builtin objects such as :class:`list` and :class:`set` handle
+self-reference by displaying "..." in the recursive part of the representation
+string.
+
+To help write such :meth:`__repr__` methods, the :mod:`reprlib` module has a new
+decorator, :func:`reprlib.recursive_repr`, for detecting recursive calls to
+:meth:`__repr__` and substituting a placeholder string instead:
+
+        >>> class MyList(list):
+                @recursive_repr()
+                def __repr__(self):
+                    return '<' + '|'.join(map(repr, self)) + '>'
+
+        >>> m = MyList('abc')
+        >>> m.append(m)
+        >>> m.append('x')
+        >>> print(m)
+        <'a'|'b'|'c'|...|'x'>
+
+(Contributed by Raymond Hettinger.)
+
 contextlib
 ----------
 
@@ -1697,9 +1723,6 @@
             - non-UTF8 percent encoding of non-ASCII characters
           Issue 2987 for IPv6 (RFC2732) support in urlparse
 
-.. XXX reprlib.recursive_repr
-
-
 Multi-threading
 ===============
 

Modified: python/branches/py3k/Lib/reprlib.py
==============================================================================
--- python/branches/py3k/Lib/reprlib.py	(original)
+++ python/branches/py3k/Lib/reprlib.py	Sun Jan 23 22:05:46 2011
@@ -30,6 +30,7 @@
         wrapper.__module__ = getattr(user_function, '__module__')
         wrapper.__doc__ = getattr(user_function, '__doc__')
         wrapper.__name__ = getattr(user_function, '__name__')
+        wrapper.__name__ = getattr(user_function, '__annotations__', {})
         return wrapper
 
     return decorating_function


More information about the Python-checkins mailing list