]> granicus.if.org Git - python/commitdiff
Add entry for reprlib.
authorRaymond Hettinger <python@rcn.com>
Sun, 23 Jan 2011 21:05:46 +0000 (21:05 +0000)
committerRaymond Hettinger <python@rcn.com>
Sun, 23 Jan 2011 21:05:46 +0000 (21:05 +0000)
Doc/library/reprlib.rst
Doc/whatsnew/3.2.rst
Lib/reprlib.py

index c794a8d389cda39e4eb869b11ef45d1aba4d778e..0e870daa3fa21b0775b2c707ff4a6fada87b5325 100644 (file)
@@ -5,6 +5,9 @@
    :synopsis: Alternate repr() implementation with size limits.
 .. sectionauthor:: Fred L. Drake, Jr. <fdrake@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
index 3c1da9f373463e352c7c7ef09c362dd85ed8bc84..c680d31640d93f3a0e19b385054d96314f05456b 100644 (file)
@@ -987,6 +987,32 @@ implemented::
 
 (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 @@ reading directly from dictionaries and strings.
             - non-UTF8 percent encoding of non-ASCII characters
           Issue 2987 for IPv6 (RFC2732) support in urlparse
 
-.. XXX reprlib.recursive_repr
-
-
 Multi-threading
 ===============
 
index 7b5c436c39e1a870839a88ebf011fc8eecae9fa4..9fee3046c69af3356b36303c303a3ddc926e0585 100644 (file)
@@ -30,6 +30,7 @@ def recursive_repr(fillvalue='...'):
         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