blob: b60418613b06a89b2c4741f9d9f737541149cc04 [file] [log] [blame]
Brett Cannon2ee0e8e2008-05-23 05:03:59 +00001:mod:`repr` --- Alternate :func:`repr` implementation
2=====================================================
Georg Brandl8ec7f652007-08-15 14:28:01 +00003
Alexandre Vassalotti50746272008-05-16 07:00:58 +00004.. module:: repr
Georg Brandl8ec7f652007-08-15 14:28:01 +00005 :synopsis: Alternate repr() implementation with size limits.
6.. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
7
Alexandre Vassalotti50746272008-05-16 07:00:58 +00008.. note::
Ezio Melotti510ff542012-05-03 19:21:40 +03009 The :mod:`repr` module has been renamed to :mod:`reprlib` in Python 3. The
Georg Brandle152a772008-05-24 18:31:28 +000010 :term:`2to3` tool will automatically adapt imports when converting your
Ezio Melotti510ff542012-05-03 19:21:40 +030011 sources to Python 3.
Alexandre Vassalotti50746272008-05-16 07:00:58 +000012
Éric Araujo29a0b572011-08-19 02:14:03 +020013**Source code:** :source:`Lib/repr.py`
14
15--------------
16
Brett Cannon2ee0e8e2008-05-23 05:03:59 +000017The :mod:`repr` module provides a means for producing object representations
Georg Brandl8ec7f652007-08-15 14:28:01 +000018with limits on the size of the resulting strings. This is used in the Python
19debugger and may be useful in other contexts as well.
20
21This module provides a class, an instance, and a function:
22
23
24.. class:: Repr()
25
26 Class which provides formatting services useful in implementing functions
Benjamin Peterson93cd25d2014-02-14 14:10:39 -050027 similar to the built-in :ref:`repr() <func-repr>`; size limits for different
28 object types are added to avoid the generation of representations which are
29 excessively long.
Georg Brandl8ec7f652007-08-15 14:28:01 +000030
31
32.. data:: aRepr
33
Georg Brandl9fa61bb2009-07-26 14:19:57 +000034 This is an instance of :class:`Repr` which is used to provide the :func:`.repr`
Georg Brandl8ec7f652007-08-15 14:28:01 +000035 function described below. Changing the attributes of this object will affect
Georg Brandl9fa61bb2009-07-26 14:19:57 +000036 the size limits used by :func:`.repr` and the Python debugger.
Georg Brandl8ec7f652007-08-15 14:28:01 +000037
38
39.. function:: repr(obj)
40
Georg Brandl9fa61bb2009-07-26 14:19:57 +000041 This is the :meth:`~Repr.repr` method of ``aRepr``. It returns a string
42 similar to that returned by the built-in function of the same name, but with
43 limits on most sizes.
Georg Brandl8ec7f652007-08-15 14:28:01 +000044
45
46.. _repr-objects:
47
48Repr Objects
49------------
50
Senthil Kumaran6f18b982011-07-04 12:50:02 -070051:class:`Repr` instances provide several attributes which can be used to provide
Georg Brandl8ec7f652007-08-15 14:28:01 +000052size limits for the representations of different object types, and methods
53which format specific object types.
54
55
56.. attribute:: Repr.maxlevel
57
58 Depth limit on the creation of recursive representations. The default is ``6``.
59
60
61.. attribute:: Repr.maxdict
62 Repr.maxlist
63 Repr.maxtuple
64 Repr.maxset
65 Repr.maxfrozenset
66 Repr.maxdeque
67 Repr.maxarray
68
69 Limits on the number of entries represented for the named object type. The
70 default is ``4`` for :attr:`maxdict`, ``5`` for :attr:`maxarray`, and ``6`` for
71 the others.
72
73 .. versionadded:: 2.4
74 :attr:`maxset`, :attr:`maxfrozenset`, and :attr:`set`.
75
76
77.. attribute:: Repr.maxlong
78
79 Maximum number of characters in the representation for a long integer. Digits
80 are dropped from the middle. The default is ``40``.
81
82
83.. attribute:: Repr.maxstring
84
85 Limit on the number of characters in the representation of the string. Note
86 that the "normal" representation of the string is used as the character source:
87 if escape sequences are needed in the representation, these may be mangled when
88 the representation is shortened. The default is ``30``.
89
90
91.. attribute:: Repr.maxother
92
93 This limit is used to control the size of object types for which no specific
94 formatting method is available on the :class:`Repr` object. It is applied in a
95 similar manner as :attr:`maxstring`. The default is ``20``.
96
97
98.. method:: Repr.repr(obj)
99
Benjamin Peterson93cd25d2014-02-14 14:10:39 -0500100 The equivalent to the built-in :ref:`repr() <func-repr>` that uses the
101 formatting imposed by the instance.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000102
103
104.. method:: Repr.repr1(obj, level)
105
Georg Brandl9fa61bb2009-07-26 14:19:57 +0000106 Recursive implementation used by :meth:`.repr`. This uses the type of *obj* to
Georg Brandl8ec7f652007-08-15 14:28:01 +0000107 determine which formatting method to call, passing it *obj* and *level*. The
108 type-specific methods should call :meth:`repr1` to perform recursive formatting,
109 with ``level - 1`` for the value of *level* in the recursive call.
110
111
112.. method:: Repr.repr_TYPE(obj, level)
113 :noindex:
114
115 Formatting methods for specific types are implemented as methods with a name
116 based on the type name. In the method name, **TYPE** is replaced by
117 ``string.join(string.split(type(obj).__name__, '_'))``. Dispatch to these
118 methods is handled by :meth:`repr1`. Type-specific methods which need to
119 recursively format a value should call ``self.repr1(subobj, level - 1)``.
120
121
122.. _subclassing-reprs:
123
124Subclassing Repr Objects
125------------------------
126
127The use of dynamic dispatching by :meth:`Repr.repr1` allows subclasses of
128:class:`Repr` to add support for additional built-in object types or to modify
129the handling of types already supported. This example shows how special support
130for file objects could be added::
131
Georg Brandle92818f2009-01-03 20:47:01 +0000132 import repr as reprlib
Georg Brandl8ec7f652007-08-15 14:28:01 +0000133 import sys
134
Georg Brandle92818f2009-01-03 20:47:01 +0000135 class MyRepr(reprlib.Repr):
Georg Brandl8ec7f652007-08-15 14:28:01 +0000136 def repr_file(self, obj, level):
137 if obj.name in ['<stdin>', '<stdout>', '<stderr>']:
138 return obj.name
139 else:
Georg Brandle92818f2009-01-03 20:47:01 +0000140 return repr(obj)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000141
142 aRepr = MyRepr()
143 print aRepr.repr(sys.stdin) # prints '<stdin>'
144