1 \section{\module{inspect
} ---
4 \declaremodule{standard
}{inspect
}
5 \modulesynopsis{Extract information and source code from live objects.
}
6 \moduleauthor{Ka-Ping Yee
}{ping@lfw.org
}
7 \sectionauthor{Ka-Ping Yee
}{ping@lfw.org
}
11 The
\module{inspect
} module provides several useful functions
12 to help get information about live objects such as modules,
13 classes, methods, functions, tracebacks, frame objects, and
14 code objects. For example, it can help you examine the
15 contents of a class, retrieve the source code of a method,
16 extract and format the argument list for a function, or
17 get all the information you need to display a detailed traceback.
19 There are four main kinds of services provided by this module:
20 type checking, getting source code, inspecting classes
21 and functions, and examining the interpreter stack.
23 \subsection{Types and members
24 \label{inspect-types
}}
26 The
\function{getmembers()
} function retrieves the members
27 of an object such as a class or module.
28 The nine functions whose names begin with ``is'' are mainly
29 provided as convenient choices for the second argument to
30 \function{getmembers()
}. They also help you determine when
31 you can expect to find the following special attributes:
33 \begin{tableiii
}{c|l|l
}{}{Type
}{Attribute
}{Description
}
34 \lineiii{module
}{__doc__
}{documentation string
}
35 \lineiii{}{__file__
}{filename (missing for built-in modules)
}
37 \lineiii{class
}{__doc__
}{documentation string
}
38 \lineiii{}{__module__
}{name of module in which this class was defined
}
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
}}
46 \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__)
}
54 \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)
}
59 \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
}}
72 \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
}
85 \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
}}
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.
96 \begin{funcdesc
}{ismodule
}{object
}
97 Return true if the object is a module.
100 \begin{funcdesc
}{isclass
}{object
}
101 Return true if the object is a class.
104 \begin{funcdesc
}{ismethod
}{object
}
105 Return true if the object is a method.
108 \begin{funcdesc
}{isfunction
}{object
}
109 Return true if the object is a Python function or unnamed (lambda) function.
112 \begin{funcdesc
}{istraceback
}{object
}
113 Return true if the object is a traceback.
116 \begin{funcdesc
}{isframe
}{object
}
117 Return true if the object is a frame.
120 \begin{funcdesc
}{iscode
}{object
}
121 Return true if the object is a code.
124 \begin{funcdesc
}{isbuiltin
}{object
}
125 Return true if the object is a built-in function.
128 \begin{funcdesc
}{isroutine
}{object
}
129 Return true if the object is a user-defined or built-in function or method.
132 \subsection{Retrieving source code
133 \label{inspect-source
}}
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.
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).
148 \begin{funcdesc
}{getfile
}{object
}
149 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.
154 \begin{funcdesc
}{getmodule
}{object
}
155 Try to guess which module an object was defined in.
158 \begin{funcdesc
}{getsourcefile
}{object
}
159 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.
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
169 original source file the first line of code was found. An
170 \exception{IOError
} is raised if the source code cannot be retrieved.
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
177 \exception{IOError
} is raised if the source code cannot be retrieved.
180 \subsection{Classes and functions
181 \label{inspect-classes-functions
}}
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.
193 \begin{funcdesc
}{getargspec
}{func
}
194 Get the names and default values of a function's arguments.
195 A tuple of four things is returned:
\code{(
\var{args
},
196 \var{varargs
},
\var{varkw
},
\var{defaults
})
}.
197 \var{args
} is a list of the argument names (it may contain nested lists).
198 \var{varargs
} and
\var{varkw
} are the names of the
\code{*
} and
199 \code{**
} arguments or
\code{None
}.
200 \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
}.
205 \begin{funcdesc
}{getargvalues
}{frame
}
206 Get information about arguments passed into a particular frame.
207 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
211 \var{varargs
} and
\var{varkw
} are the names of the
\code{*
} and
212 \code{**
} arguments or
\code{None
}.
213 \var{locals
} is the locals dictionary of the given frame.
216 \begin{funcdesc
}{formatargspec
}{args
\optional{, varargs, varkw, defaults,
217 argformat, varargsformat, varkwformat, defaultformat
}}
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.
225 \begin{funcdesc
}{formatargvalues
}{args
\optional{, varargs, varkw, locals,
226 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.
233 \subsection{The interpreter stack
234 \label{inspect-stack
}}
236 When the following functions return ``frame records,'' each record
237 is a tuple of six items: the frame object, the filename,
238 the line number of the current line, the function name, a list of
239 lines of context from the source code, and the index of the current
240 line within that list.
241 The optional
\var{context
} argument specifies the number of lines of
242 context to return, which are centered around the current line.
244 \begin{funcdesc
}{getouterframes
}{frame
\optional{, context
}}
245 Get a list of frame records for a frame and all higher (calling)
249 \begin{funcdesc
}{getinnerframes
}{traceback
\optional{, context
}}
250 Get a list of frame records for a traceback's frame and all lower
254 \begin{funcdesc
}{currentframe
}{}
255 Return the frame object for the caller's stack frame.
258 \begin{funcdesc
}{stack
}{\optional{context
}}
259 Return a list of frame records for the stack above the caller's
263 \begin{funcdesc
}{trace
}{\optional{context
}}
264 Return a list of frame records for the stack below the current