File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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
912The :mod: `reprlib ` module provides a means for producing object representations
1013with limits on the size of the resulting strings. This is used in the Python
Original file line number Diff line number Diff 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+
9901016contextlib
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
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments