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