Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 98b140c

Browse files
committed
Add entry for reprlib.
1 parent e6d4c5b commit 98b140c

3 files changed

Lines changed: 30 additions & 3 deletions

File tree

Doc/library/reprlib.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
:synopsis: Alternate repr() implementation with size limits.
66
.. sectionauthor:: Fred L. Drake, Jr. <[email protected]>
77

8+
**Source code:** :source:`Lib/reprlib.py`
9+
10+
--------------
811

912
The :mod:`reprlib` module provides a means for producing object representations
1013
with limits on the size of the resulting strings. This is used in the Python

Doc/whatsnew/3.2.rst

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -987,6 +987,32 @@ implemented::
987987

988988
(Patch submitted by Daniel Urban; :issue:`5867`.)
989989

990+
reprlib
991+
-------
992+
993+
When writing a :meth:`__repr__` method for a custom container, it is easy to
994+
forget to handle the case where a member refers back to the container itself.
995+
Python's builtin objects such as :class:`list` and :class:`set` handle
996+
self-reference by displaying "..." in the recursive part of the representation
997+
string.
998+
999+
To help write such :meth:`__repr__` methods, the :mod:`reprlib` module has a new
1000+
decorator, :func:`reprlib.recursive_repr`, for detecting recursive calls to
1001+
:meth:`__repr__` and substituting a placeholder string instead:
1002+
1003+
>>> class MyList(list):
1004+
@recursive_repr()
1005+
def __repr__(self):
1006+
return '<' + '|'.join(map(repr, self)) + '>'
1007+
1008+
>>> m = MyList('abc')
1009+
>>> m.append(m)
1010+
>>> m.append('x')
1011+
>>> print(m)
1012+
<'a'|'b'|'c'|...|'x'>
1013+
1014+
(Contributed by Raymond Hettinger.)
1015+
9901016
contextlib
9911017
----------
9921018

@@ -1697,9 +1723,6 @@ reading directly from dictionaries and strings.
16971723
- non-UTF8 percent encoding of non-ASCII characters
16981724
Issue 2987 for IPv6 (RFC2732) support in urlparse
16991725
1700-
.. XXX reprlib.recursive_repr
1701-
1702-
17031726
Multi-threading
17041727
===============
17051728

Lib/reprlib.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ def wrapper(self):
3030
wrapper.__module__ = getattr(user_function, '__module__')
3131
wrapper.__doc__ = getattr(user_function, '__doc__')
3232
wrapper.__name__ = getattr(user_function, '__name__')
33+
wrapper.__name__ = getattr(user_function, '__annotations__', {})
3334
return wrapper
3435

3536
return decorating_function

0 commit comments

Comments
 (0)