blob: 11e6ae20ca78c02a36e73ad58fd11c83db8777eb [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::
Georg Brandle152a772008-05-24 18:31:28 +00009 The :mod:`repr` module has been renamed to :mod:`reprlib` in Python 3.0. The
10 :term:`2to3` tool will automatically adapt imports when converting your
11 sources to 3.0.
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
27 similar to the built-in :func:`repr`; size limits for different object types
28 are added to avoid the generation of representations which are excessively long.
29
30
31.. data:: aRepr
32
Georg Brandl9fa61bb2009-07-26 14:19:57 +000033 This is an instance of :class:`Repr` which is used to provide the :func:`.repr`
Georg Brandl8ec7f652007-08-15 14:28:01 +000034 function described below. Changing the attributes of this object will affect
Georg Brandl9fa61bb2009-07-26 14:19:57 +000035 the size limits used by :func:`.repr` and the Python debugger.
Georg Brandl8ec7f652007-08-15 14:28:01 +000036
37
38.. function:: repr(obj)
39
Georg Brandl9fa61bb2009-07-26 14:19:57 +000040 This is the :meth:`~Repr.repr` method of ``aRepr``. It returns a string
41 similar to that returned by the built-in function of the same name, but with
42 limits on most sizes.
Georg Brandl8ec7f652007-08-15 14:28:01 +000043
44
45.. _repr-objects:
46
47Repr Objects
48------------
49
Senthil Kumaran6f18b982011-07-04 12:50:02 -070050:class:`Repr` instances provide several attributes which can be used to provide
Georg Brandl8ec7f652007-08-15 14:28:01 +000051size limits for the representations of different object types, and methods
52which format specific object types.
53
54
55.. attribute:: Repr.maxlevel
56
57 Depth limit on the creation of recursive representations. The default is ``6``.
58
59
60.. attribute:: Repr.maxdict
61 Repr.maxlist
62 Repr.maxtuple
63 Repr.maxset
64 Repr.maxfrozenset
65 Repr.maxdeque
66 Repr.maxarray
67
68 Limits on the number of entries represented for the named object type. The
69 default is ``4`` for :attr:`maxdict`, ``5`` for :attr:`maxarray`, and ``6`` for
70 the others.
71
72 .. versionadded:: 2.4
73 :attr:`maxset`, :attr:`maxfrozenset`, and :attr:`set`.
74
75
76.. attribute:: Repr.maxlong
77
78 Maximum number of characters in the representation for a long integer. Digits
79 are dropped from the middle. The default is ``40``.
80
81
82.. attribute:: Repr.maxstring
83
84 Limit on the number of characters in the representation of the string. Note
85 that the "normal" representation of the string is used as the character source:
86 if escape sequences are needed in the representation, these may be mangled when
87 the representation is shortened. The default is ``30``.
88
89
90.. attribute:: Repr.maxother
91
92 This limit is used to control the size of object types for which no specific
93 formatting method is available on the :class:`Repr` object. It is applied in a
94 similar manner as :attr:`maxstring`. The default is ``20``.
95
96
97.. method:: Repr.repr(obj)
98
99 The equivalent to the built-in :func:`repr` that uses the formatting imposed by
100 the instance.
101
102
103.. method:: Repr.repr1(obj, level)
104
Georg Brandl9fa61bb2009-07-26 14:19:57 +0000105 Recursive implementation used by :meth:`.repr`. This uses the type of *obj* to
Georg Brandl8ec7f652007-08-15 14:28:01 +0000106 determine which formatting method to call, passing it *obj* and *level*. The
107 type-specific methods should call :meth:`repr1` to perform recursive formatting,
108 with ``level - 1`` for the value of *level* in the recursive call.
109
110
111.. method:: Repr.repr_TYPE(obj, level)
112 :noindex:
113
114 Formatting methods for specific types are implemented as methods with a name
115 based on the type name. In the method name, **TYPE** is replaced by
116 ``string.join(string.split(type(obj).__name__, '_'))``. Dispatch to these
117 methods is handled by :meth:`repr1`. Type-specific methods which need to
118 recursively format a value should call ``self.repr1(subobj, level - 1)``.
119
120
121.. _subclassing-reprs:
122
123Subclassing Repr Objects
124------------------------
125
126The use of dynamic dispatching by :meth:`Repr.repr1` allows subclasses of
127:class:`Repr` to add support for additional built-in object types or to modify
128the handling of types already supported. This example shows how special support
129for file objects could be added::
130
Georg Brandle92818f2009-01-03 20:47:01 +0000131 import repr as reprlib
Georg Brandl8ec7f652007-08-15 14:28:01 +0000132 import sys
133
Georg Brandle92818f2009-01-03 20:47:01 +0000134 class MyRepr(reprlib.Repr):
Georg Brandl8ec7f652007-08-15 14:28:01 +0000135 def repr_file(self, obj, level):
136 if obj.name in ['<stdin>', '<stdout>', '<stderr>']:
137 return obj.name
138 else:
Georg Brandle92818f2009-01-03 20:47:01 +0000139 return repr(obj)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000140
141 aRepr = MyRepr()
142 print aRepr.repr(sys.stdin) # prints '<stdin>'
143