blob: 76bf9420f48806f312dedd41ea94ac965521f6d4 [file] [log] [blame]
Fred Drake9d814c61999-01-27 17:20:33 +00001\section{\module{repr} ---
2 Alternate \function{repr()} implementation.}
3
4\declaremodule{standard}{repr}
5
6
7The \module{repr} module provides a means for producing object
8representations with limits on the size of the resulting strings.
9This is used in the Python debugger and may be useful in other
10contexts as well.
11
12This module provides a class, an instance, and a function:
13
14
15\begin{classdesc}{Repr}{}
16 Class which provides formatting services useful in implementing
17 functions similar to the built-in \function{repr()}; size limits for
18 different object types are added to avoid the generation of
19 representations which are excessively long.
20\end{classdesc}
21
22
23\begin{datadesc}{aRepr}
24 This is an instance of \class{Repr} which is used to provide the
25 \function{repr()} function described below. Changing the attributes
26 of this object will affect the size limits used by \function{repr()}
27 and the Python debugger.
28\end{datadesc}
29
30
31\begin{funcdesc}{repr}{obj}
32 This is the \method{repr()} method of \code{aRepr}. It returns a
33 string similar to that returned by the built-in function of the same
34 name, but with limits on most sizes.
35\end{funcdesc}
36
37
38\subsection{Repr Objects \label{Repr-objects}}
39
40\class{Repr} instances provide several members which can be used to
41provide size limits for the representations of different object types,
42and methods which format specific object types.
43
44
45\begin{memberdesc}{maxlevel}
46 Depth limit on the creation of recursive representations. The
47 default is \code{6}.
48\end{memberdesc}
49
50\begin{memberdesc}{maxdict}
51\memberline{maxlist}
52\memberline{maxtuple}
53 Limits on the number of entries represented for the named object
54 type. The default for \member{maxdict} is \code{4}, for the others,
55 \code{6}.
56\end{memberdesc}
57
58\begin{memberdesc}{maxlong}
59 Maximum number of characters in the representation for a long
60 integer. Digits are dropped from the middle. The default is
61 \code{40}.
62\end{memberdesc}
63
64\begin{memberdesc}{maxstring}
65 Limit on the number of characters in the representation of the
66 string. Note that the ``normal'' representation of the string is
67 used as the character source: if escape sequences are needed in the
68 representation, these may be mangled when the representation is
69 shortened. The default is \code{30}.
70\end{memberdesc}
71
72\begin{memberdesc}{maxother}
73 This limit is used to control the size of object types for which no
74 specific formatting method is available on the \class{Repr} object.
75 It is applied in a similar manner as \member{maxstring}. The
76 default is \code{20}.
77\end{memberdesc}
78
79\begin{methoddesc}{repr}{obj}
80 The equivalent to the built-in \function{repr()} that uses the
81 formatting imposed by the instance.
82\end{methoddesc}
83
84\begin{methoddesc}{repr1}{obj, level}
85 Recursive implementation used by \method{repr()}. This uses the
86 type of \var{obj} to determine which formatting method to call,
87 passing it \var{obj} and \var{level}. The type-specific methods
88 should call \method{repr1()} to perform recursive formatting, with
89 \code{\var{level} - 1} for the value of \var{level} in the recursive
90 call.
91\end{methoddesc}
92
93\begin{methoddescni}{repr_\var{type}}{obj, level}
94 Formatting methods for specific types are implemented as methods
95 with a name based on the type name. In the method name, \var{type}
96 is replaced by
97 \code{string.join(string.split(type(\var{obj}).__name__, '_')}.
98 Dispatch to these methods is handled by \method{repr1()}.
99 Type-specific methods which need to recursively format a value
100 should call \samp{self.repr1(\var{subobj}, \var{level} - 1)}.
101\end{methoddescni}
102
103
104\subsection{Subclassing Repr Objects \label{subclassing-reprs}}
105
106The use of dynamic dispatching by \method{Repr.repr1()} allows
107subclasses of \class{Repr} to add support for additional built-in
108object types or to modify the handling of types already supported.
109This example shows how special support for file objects could be
110added:
111
112\begin{verbatim}
113import repr
114import sys
115
116class MyRepr(repr.Repr):
117 def repr_file(self, obj, level):
118 if obj.name in ['<stdin>', '<stdout>', '<stderr>']:
119 return obj.name
120 else:
121 return `obj`
122
123aRepr = MyRepr()
124print aRepr.repr(sys.stdin) # prints '<stdin>'
125\end{verbatim}