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