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