blob: 38cca7488d2a04e89127d2389318abab93510e5d [file] [log] [blame]
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001\section{\module{inspect} ---
2 Inspect live objects}
Fred Drake6dbd3822001-02-28 23:01:38 +00003
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00004\declaremodule{standard}{inspect}
5\modulesynopsis{Extract information and source code from live objects.}
Fred Drake6dbd3822001-02-28 23:01:38 +00006\moduleauthor{Ka-Ping Yee}{ping@lfw.org}
7\sectionauthor{Ka-Ping Yee}{ping@lfw.org}
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00008
9\versionadded{2.1}
10
Fred Drake6dbd3822001-02-28 23:01:38 +000011The \module{inspect} module provides several useful functions
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000012to help get information about live objects such as modules,
13classes, methods, functions, tracebacks, frame objects, and
14code objects. For example, it can help you examine the
15contents of a class, retrieve the source code of a method,
16extract and format the argument list for a function, or
17get all the information you need to display a detailed traceback.
18
19There are four main kinds of services provided by this module:
20type checking, getting source code, inspecting classes
21and functions, and examining the interpreter stack.
22
23\subsection{Types and members
24 \label{inspect-types}}
25
26The \function{getmembers()} function retrieves the members
27of an object such as a class or module.
28The nine functions whose names begin with ``is'' are mainly
29provided as convenient choices for the second argument to
30\function{getmembers()}. They also help you determine when
31you can expect to find the following special attributes:
32
33\begin{tableiii}{c|l|l}{}{Type}{Attribute}{Description}
34 \lineiii{module}{__doc__}{documentation string}
35 \lineiii{}{__file__}{filename (missing for built-in modules)}
Fred Drake6dbd3822001-02-28 23:01:38 +000036 \hline
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000037 \lineiii{class}{__doc__}{documentation string}
38 \lineiii{}{__module__}{name of module in which this class was defined}
Fred Drake6dbd3822001-02-28 23:01:38 +000039 \hline
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000040 \lineiii{method}{__doc__}{documentation string}
41 \lineiii{}{__name__}{name with which this method was defined}
42 \lineiii{}{im_class}{class object in which this method belongs}
43 \lineiii{}{im_func}{function object containing implementation of method}
44 \lineiii{}{im_self}{instance to which this method is bound, or \code{None}}
Fred Drake6dbd3822001-02-28 23:01:38 +000045 \hline
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000046 \lineiii{function}{__doc__}{documentation string}
47 \lineiii{}{__name__}{name with which this function was defined}
48 \lineiii{}{func_code}{code object containing compiled function bytecode}
49 \lineiii{}{func_defaults}{tuple of any default values for arguments}
50 \lineiii{}{func_doc}{(same as __doc__)}
51 \lineiii{}{func_globals}{global namespace in which this function was defined}
52 \lineiii{}{func_name}{(same as __name__)}
Fred Drake6dbd3822001-02-28 23:01:38 +000053 \hline
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000054 \lineiii{traceback}{tb_frame}{frame object at this level}
55 \lineiii{}{tb_lasti}{index of last attempted instruction in bytecode}
56 \lineiii{}{tb_lineno}{current line number in Python source code}
57 \lineiii{}{tb_next}{next inner traceback object (called by this level)}
Fred Drake6dbd3822001-02-28 23:01:38 +000058 \hline
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000059 \lineiii{frame}{f_back}{next outer frame object (this frame's caller)}
60 \lineiii{}{f_builtins}{built-in namespace seen by this frame}
61 \lineiii{}{f_code}{code object being executed in this frame}
62 \lineiii{}{f_exc_traceback}{traceback if raised in this frame, or \code{None}}
63 \lineiii{}{f_exc_type}{exception type if raised in this frame, or \code{None}}
64 \lineiii{}{f_exc_value}{exception value if raised in this frame, or \code{None}}
65 \lineiii{}{f_globals}{global namespace seen by this frame}
66 \lineiii{}{f_lasti}{index of last attempted instruction in bytecode}
67 \lineiii{}{f_lineno}{current line number in Python source code}
68 \lineiii{}{f_locals}{local namespace seen by this frame}
69 \lineiii{}{f_restricted}{0 or 1 if frame is in restricted execution mode}
70 \lineiii{}{f_trace}{tracing function for this frame, or \code{None}}
Fred Drake6dbd3822001-02-28 23:01:38 +000071 \hline
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000072 \lineiii{code}{co_argcount}{number of arguments (not including * or ** args)}
73 \lineiii{}{co_code}{string of raw compiled bytecode}
74 \lineiii{}{co_consts}{tuple of constants used in the bytecode}
75 \lineiii{}{co_filename}{name of file in which this code object was created}
76 \lineiii{}{co_firstlineno}{number of first line in Python source code}
77 \lineiii{}{co_flags}{bitmap: 1=optimized \code{|} 2=newlocals \code{|} 4=*arg \code{|} 8=**arg}
78 \lineiii{}{co_lnotab}{encoded mapping of line numbers to bytecode indices}
79 \lineiii{}{co_name}{name with which this code object was defined}
80 \lineiii{}{co_names}{tuple of names of local variables}
81 \lineiii{}{co_nlocals}{number of local variables}
82 \lineiii{}{co_stacksize}{virtual machine stack space required}
83 \lineiii{}{co_varnames}{tuple of names of arguments and local variables}
Fred Drake6dbd3822001-02-28 23:01:38 +000084 \hline
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000085 \lineiii{builtin}{__doc__}{documentation string}
86 \lineiii{}{__name__}{original name of this function or method}
87 \lineiii{}{__self__}{instance to which a method is bound, or \code{None}}
88\end{tableiii}
89
90\begin{funcdesc}{getmembers}{object\optional{, predicate}}
91 Return all the members of an object in a list of (name, value) pairs
92 sorted by name. If the optional \var{predicate} argument is supplied,
93 only members for which the predicate returns a true value are included.
94\end{funcdesc}
95
96\begin{funcdesc}{ismodule}{object}
97 Return true if the object is a module.
98\end{funcdesc}
99
100\begin{funcdesc}{isclass}{object}
101 Return true if the object is a class.
102\end{funcdesc}
103
104\begin{funcdesc}{ismethod}{object}
105 Return true if the object is a method.
106\end{funcdesc}
107
108\begin{funcdesc}{isfunction}{object}
109 Return true if the object is a Python function or unnamed (lambda) function.
110\end{funcdesc}
111
112\begin{funcdesc}{istraceback}{object}
113 Return true if the object is a traceback.
114\end{funcdesc}
115
116\begin{funcdesc}{isframe}{object}
117 Return true if the object is a frame.
118\end{funcdesc}
119
120\begin{funcdesc}{iscode}{object}
121 Return true if the object is a code.
122\end{funcdesc}
123
124\begin{funcdesc}{isbuiltin}{object}
125 Return true if the object is a built-in function.
126\end{funcdesc}
127
128\begin{funcdesc}{isroutine}{object}
129 Return true if the object is a user-defined or built-in function or method.
130\end{funcdesc}
131
132\subsection{Retrieving source code
133 \label{inspect-source}}
134
135\begin{funcdesc}{getdoc}{object}
136 Get the documentation string for an object.
137 All tabs are expanded to spaces. To clean up docstrings that are
138 indented to line up with blocks of code, any whitespace than can be
139 uniformly removed from the second line onwards is removed.
140\end{funcdesc}
141
142\begin{funcdesc}{getcomments}{object}
143 Return in a single string any lines of comments immediately preceding
144 the object's source code (for a class, function, or method), or at the
145 top of the Python source file (if the object is a module).
146\end{funcdesc}
147
148\begin{funcdesc}{getfile}{object}
Fred Drake6dbd3822001-02-28 23:01:38 +0000149 Return the name of the (text or binary) file in which an object was
150 defined. This will fail with a \exception{TypeError} if the object
151 is a built-in module, class, or function.
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000152\end{funcdesc}
153
154\begin{funcdesc}{getmodule}{object}
155 Try to guess which module an object was defined in.
156\end{funcdesc}
157
158\begin{funcdesc}{getsourcefile}{object}
Fred Drake6dbd3822001-02-28 23:01:38 +0000159 Return the name of the Python source file in which an object was
160 defined. This will fail with a \exception{TypeError} if the object
161 is a built-in module, class, or function.
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000162\end{funcdesc}
163
164\begin{funcdesc}{getsourcelines}{object}
165 Return a list of source lines and starting line number for an object.
166 The argument may be a module, class, method, function, traceback, frame,
167 or code object. The source code is returned as a list of the lines
168 corresponding to the object and the line number indicates where in the
Fred Drake6dbd3822001-02-28 23:01:38 +0000169 original source file the first line of code was found. An
170 \exception{IOError} is raised if the source code cannot be retrieved.
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000171\end{funcdesc}
172
173\begin{funcdesc}{getsource}{object}
174 Return the text of the source code for an object.
175 The argument may be a module, class, method, function, traceback, frame,
176 or code object. The source code is returned as a single string. An
Fred Drake6dbd3822001-02-28 23:01:38 +0000177 \exception{IOError} is raised if the source code cannot be retrieved.
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000178\end{funcdesc}
179
180\subsection{Classes and functions
181 \label{inspect-classes-functions}}
182
183\begin{funcdesc}{getclasstree}{classes\optional{, unique}}
184 Arrange the given list of classes into a hierarchy of nested lists.
185 Where a nested list appears, it contains classes derived from the class
186 whose entry immediately precedes the list. Each entry is a 2-tuple
187 containing a class and a tuple of its base classes. If the \var{unique}
188 argument is true, exactly one entry appears in the returned structure
189 for each class in the given list. Otherwise, classes using multiple
190 inheritance and their descendants will appear multiple times.
191\end{funcdesc}
192
193\begin{funcdesc}{getargspec}{func}
194 Get the names and default values of a function's arguments.
Fred Drake6dbd3822001-02-28 23:01:38 +0000195 A tuple of four things is returned: \code{(\var{args},
196 \var{varargs}, \var{varkw}, \var{defaults})}.
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000197 \var{args} is a list of the argument names (it may contain nested lists).
Fred Drake6dbd3822001-02-28 23:01:38 +0000198 \var{varargs} and \var{varkw} are the names of the \code{*} and
199 \code{**} arguments or \code{None}.
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000200 \var{defaults} is a tuple of default argument values; if this tuple
201 has \var{n} elements, they correspond to the last \var{n} elements
202 listed in \var{args}.
203\end{funcdesc}
204
205\begin{funcdesc}{getargvalues}{frame}
206 Get information about arguments passed into a particular frame.
Fred Drake6dbd3822001-02-28 23:01:38 +0000207 A tuple of four things is returned: \code{(\var{args},
208 \var{varargs}, \var{varkw}, \var{locals})}.
209 \var{args} is a list of the argument names (it may contain nested
210 lists).
211 \var{varargs} and \var{varkw} are the names of the \code{*} and
212 \code{**} arguments or \code{None}.
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000213 \var{locals} is the locals dictionary of the given frame.
214\end{funcdesc}
215
216\begin{funcdesc}{formatargspec}{args\optional{, varargs, varkw, defaults,
Fred Drake6dbd3822001-02-28 23:01:38 +0000217 argformat, varargsformat, varkwformat, defaultformat}}
218
219 Format a pretty argument spec from the four values returned by
220 \function{getargspec()}. The other four arguments are the
221 corresponding optional formatting functions that are called to turn
222 names and values into strings.
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000223\end{funcdesc}
224
225\begin{funcdesc}{formatargvalues}{args\optional{, varargs, varkw, locals,
Fred Drake6dbd3822001-02-28 23:01:38 +0000226 argformat, varargsformat, varkwformat, valueformat}}
227 Format a pretty argument spec from the four values returned by
228 \function{getargvalues()}. The other four arguments are the
229 corresponding optional formatting functions that are called to turn
230 names and values into strings.
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000231\end{funcdesc}
232
233\subsection{The interpreter stack
234 \label{inspect-stack}}
235
Fred Drake6dbd3822001-02-28 23:01:38 +0000236When the following functions return ``frame records,'' each record
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000237is a tuple of six items: the frame object, the filename,
238the line number of the current line, the function name, a list of
239lines of context from the source code, and the index of the current
240line within that list.
241The optional \var{context} argument specifies the number of lines of
242context to return, which are centered around the current line.
243
244\begin{funcdesc}{getouterframes}{frame\optional{, context}}
Fred Drake6dbd3822001-02-28 23:01:38 +0000245 Get a list of frame records for a frame and all higher (calling)
246 frames.
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000247\end{funcdesc}
248
249\begin{funcdesc}{getinnerframes}{traceback\optional{, context}}
Fred Drake6dbd3822001-02-28 23:01:38 +0000250 Get a list of frame records for a traceback's frame and all lower
251 frames.
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000252\end{funcdesc}
253
254\begin{funcdesc}{currentframe}{}
255 Return the frame object for the caller's stack frame.
256\end{funcdesc}
257
258\begin{funcdesc}{stack}{\optional{context}}
Fred Drake6dbd3822001-02-28 23:01:38 +0000259 Return a list of frame records for the stack above the caller's
260 frame.
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000261\end{funcdesc}
262
263\begin{funcdesc}{trace}{\optional{context}}
Fred Drake6dbd3822001-02-28 23:01:38 +0000264 Return a list of frame records for the stack below the current
265 exception.
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000266\end{funcdesc}