blob: 6101ad87e0c279e8bbde3f043c7f5ea4bc1bba0e [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
Fred Drake90a72f82001-04-10 15:12:34 +000096\begin{funcdesc}{getmoduleinfo}{path}
97 Return a tuple of values that describe how Python will interpret the
98 file identified by \var{path} if it is a module, or \code{None} if
99 it would not be identified as a module. The return tuple is
100 \code{(\var{name}, \var{suffix}, \var{mode}, \var{mtype})}, where
101 \var{name} is the name of the module without the name of any
102 enclosing package, \var{suffix} is the trailing part of the file
103 name (which may not be a dot-delimited extension), \var{mode} is the
104 \function{open()} mode that would be used (\code{'r'} or
105 \code{'rb'}), and \var{mtype} is an integer giving the type of the
106 module. \var{mtype} will have a value which can be compared to the
107 constants defined in the \refmodule{imp} module; see the
108 documentation for that module for more information on module types.
109\end{funcdesc}
110
111\begin{funcdesc}{getmodulename}{path}
112 Return the name of the module named by the file \var{path}, without
113 including the names of enclosing packages. This uses the same
114 algortihm as the interpreter uses when searching for modules. If
115 the name cannot be matched according to the interpreter's rules,
116 \code{None} is returned.
117\end{funcdesc}
118
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000119\begin{funcdesc}{ismodule}{object}
120 Return true if the object is a module.
121\end{funcdesc}
122
123\begin{funcdesc}{isclass}{object}
124 Return true if the object is a class.
125\end{funcdesc}
126
127\begin{funcdesc}{ismethod}{object}
128 Return true if the object is a method.
129\end{funcdesc}
130
131\begin{funcdesc}{isfunction}{object}
132 Return true if the object is a Python function or unnamed (lambda) function.
133\end{funcdesc}
134
135\begin{funcdesc}{istraceback}{object}
136 Return true if the object is a traceback.
137\end{funcdesc}
138
139\begin{funcdesc}{isframe}{object}
140 Return true if the object is a frame.
141\end{funcdesc}
142
143\begin{funcdesc}{iscode}{object}
144 Return true if the object is a code.
145\end{funcdesc}
146
147\begin{funcdesc}{isbuiltin}{object}
148 Return true if the object is a built-in function.
149\end{funcdesc}
150
151\begin{funcdesc}{isroutine}{object}
152 Return true if the object is a user-defined or built-in function or method.
153\end{funcdesc}
154
155\subsection{Retrieving source code
156 \label{inspect-source}}
157
158\begin{funcdesc}{getdoc}{object}
159 Get the documentation string for an object.
160 All tabs are expanded to spaces. To clean up docstrings that are
161 indented to line up with blocks of code, any whitespace than can be
162 uniformly removed from the second line onwards is removed.
163\end{funcdesc}
164
165\begin{funcdesc}{getcomments}{object}
166 Return in a single string any lines of comments immediately preceding
167 the object's source code (for a class, function, or method), or at the
168 top of the Python source file (if the object is a module).
169\end{funcdesc}
170
171\begin{funcdesc}{getfile}{object}
Fred Drake6dbd3822001-02-28 23:01:38 +0000172 Return the name of the (text or binary) file in which an object was
173 defined. This will fail with a \exception{TypeError} if the object
174 is a built-in module, class, or function.
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000175\end{funcdesc}
176
177\begin{funcdesc}{getmodule}{object}
178 Try to guess which module an object was defined in.
179\end{funcdesc}
180
181\begin{funcdesc}{getsourcefile}{object}
Fred Drake6dbd3822001-02-28 23:01:38 +0000182 Return the name of the Python source file in which an object was
183 defined. This will fail with a \exception{TypeError} if the object
184 is a built-in module, class, or function.
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000185\end{funcdesc}
186
187\begin{funcdesc}{getsourcelines}{object}
188 Return a list of source lines and starting line number for an object.
189 The argument may be a module, class, method, function, traceback, frame,
190 or code object. The source code is returned as a list of the lines
191 corresponding to the object and the line number indicates where in the
Fred Drake6dbd3822001-02-28 23:01:38 +0000192 original source file the first line of code was found. An
193 \exception{IOError} is raised if the source code cannot be retrieved.
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000194\end{funcdesc}
195
196\begin{funcdesc}{getsource}{object}
197 Return the text of the source code for an object.
198 The argument may be a module, class, method, function, traceback, frame,
199 or code object. The source code is returned as a single string. An
Fred Drake6dbd3822001-02-28 23:01:38 +0000200 \exception{IOError} is raised if the source code cannot be retrieved.
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000201\end{funcdesc}
202
203\subsection{Classes and functions
204 \label{inspect-classes-functions}}
205
206\begin{funcdesc}{getclasstree}{classes\optional{, unique}}
207 Arrange the given list of classes into a hierarchy of nested lists.
208 Where a nested list appears, it contains classes derived from the class
209 whose entry immediately precedes the list. Each entry is a 2-tuple
210 containing a class and a tuple of its base classes. If the \var{unique}
211 argument is true, exactly one entry appears in the returned structure
212 for each class in the given list. Otherwise, classes using multiple
213 inheritance and their descendants will appear multiple times.
214\end{funcdesc}
215
216\begin{funcdesc}{getargspec}{func}
217 Get the names and default values of a function's arguments.
Fred Drake6dbd3822001-02-28 23:01:38 +0000218 A tuple of four things is returned: \code{(\var{args},
219 \var{varargs}, \var{varkw}, \var{defaults})}.
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000220 \var{args} is a list of the argument names (it may contain nested lists).
Fred Drake6dbd3822001-02-28 23:01:38 +0000221 \var{varargs} and \var{varkw} are the names of the \code{*} and
222 \code{**} arguments or \code{None}.
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000223 \var{defaults} is a tuple of default argument values; if this tuple
224 has \var{n} elements, they correspond to the last \var{n} elements
225 listed in \var{args}.
226\end{funcdesc}
227
228\begin{funcdesc}{getargvalues}{frame}
229 Get information about arguments passed into a particular frame.
Fred Drake6dbd3822001-02-28 23:01:38 +0000230 A tuple of four things is returned: \code{(\var{args},
231 \var{varargs}, \var{varkw}, \var{locals})}.
232 \var{args} is a list of the argument names (it may contain nested
233 lists).
234 \var{varargs} and \var{varkw} are the names of the \code{*} and
235 \code{**} arguments or \code{None}.
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000236 \var{locals} is the locals dictionary of the given frame.
237\end{funcdesc}
238
239\begin{funcdesc}{formatargspec}{args\optional{, varargs, varkw, defaults,
Fred Drake6dbd3822001-02-28 23:01:38 +0000240 argformat, varargsformat, varkwformat, defaultformat}}
241
242 Format a pretty argument spec from the four values returned by
243 \function{getargspec()}. The other four arguments are the
244 corresponding optional formatting functions that are called to turn
245 names and values into strings.
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000246\end{funcdesc}
247
248\begin{funcdesc}{formatargvalues}{args\optional{, varargs, varkw, locals,
Fred Drake6dbd3822001-02-28 23:01:38 +0000249 argformat, varargsformat, varkwformat, valueformat}}
250 Format a pretty argument spec from the four values returned by
251 \function{getargvalues()}. The other four arguments are the
252 corresponding optional formatting functions that are called to turn
253 names and values into strings.
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000254\end{funcdesc}
255
256\subsection{The interpreter stack
257 \label{inspect-stack}}
258
Fred Drake6dbd3822001-02-28 23:01:38 +0000259When the following functions return ``frame records,'' each record
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000260is a tuple of six items: the frame object, the filename,
261the line number of the current line, the function name, a list of
262lines of context from the source code, and the index of the current
263line within that list.
264The optional \var{context} argument specifies the number of lines of
265context to return, which are centered around the current line.
266
267\begin{funcdesc}{getouterframes}{frame\optional{, context}}
Fred Drake6dbd3822001-02-28 23:01:38 +0000268 Get a list of frame records for a frame and all higher (calling)
269 frames.
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000270\end{funcdesc}
271
272\begin{funcdesc}{getinnerframes}{traceback\optional{, context}}
Fred Drake6dbd3822001-02-28 23:01:38 +0000273 Get a list of frame records for a traceback's frame and all lower
274 frames.
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000275\end{funcdesc}
276
277\begin{funcdesc}{currentframe}{}
278 Return the frame object for the caller's stack frame.
279\end{funcdesc}
280
281\begin{funcdesc}{stack}{\optional{context}}
Fred Drake6dbd3822001-02-28 23:01:38 +0000282 Return a list of frame records for the stack above the caller's
283 frame.
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000284\end{funcdesc}
285
286\begin{funcdesc}{trace}{\optional{context}}
Fred Drake6dbd3822001-02-28 23:01:38 +0000287 Return a list of frame records for the stack below the current
288 exception.
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000289\end{funcdesc}