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
}{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.
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.
119 \begin{funcdesc
}{ismodule
}{object
}
120 Return true if the object is a module.
123 \begin{funcdesc
}{isclass
}{object
}
124 Return true if the object is a class.
127 \begin{funcdesc
}{ismethod
}{object
}
128 Return true if the object is a method.
131 \begin{funcdesc
}{isfunction
}{object
}
132 Return true if the object is a Python function or unnamed (lambda) function.
135 \begin{funcdesc
}{istraceback
}{object
}
136 Return true if the object is a traceback.
139 \begin{funcdesc
}{isframe
}{object
}
140 Return true if the object is a frame.
143 \begin{funcdesc
}{iscode
}{object
}
144 Return true if the object is a code.
147 \begin{funcdesc
}{isbuiltin
}{object
}
148 Return true if the object is a built-in function.
151 \begin{funcdesc
}{isroutine
}{object
}
152 Return true if the object is a user-defined or built-in function or method.
155 \subsection{Retrieving source code
156 \label{inspect-source
}}
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.
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).
171 \begin{funcdesc
}{getfile
}{object
}
172 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.
177 \begin{funcdesc
}{getmodule
}{object
}
178 Try to guess which module an object was defined in.
181 \begin{funcdesc
}{getsourcefile
}{object
}
182 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.
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
192 original source file the first line of code was found. An
193 \exception{IOError
} is raised if the source code cannot be retrieved.
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
200 \exception{IOError
} is raised if the source code cannot be retrieved.
203 \subsection{Classes and functions
204 \label{inspect-classes-functions
}}
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.
216 \begin{funcdesc
}{getargspec
}{func
}
217 Get the names and default values of a function's arguments.
218 A tuple of four things is returned:
\code{(
\var{args
},
219 \var{varargs
},
\var{varkw
},
\var{defaults
})
}.
220 \var{args
} is a list of the argument names (it may contain nested lists).
221 \var{varargs
} and
\var{varkw
} are the names of the
\code{*
} and
222 \code{**
} arguments or
\code{None
}.
223 \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
}.
228 \begin{funcdesc
}{getargvalues
}{frame
}
229 Get information about arguments passed into a particular frame.
230 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
234 \var{varargs
} and
\var{varkw
} are the names of the
\code{*
} and
235 \code{**
} arguments or
\code{None
}.
236 \var{locals
} is the locals dictionary of the given frame.
239 \begin{funcdesc
}{formatargspec
}{args
\optional{, varargs, varkw, defaults,
240 argformat, varargsformat, varkwformat, defaultformat
}}
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.
248 \begin{funcdesc
}{formatargvalues
}{args
\optional{, varargs, varkw, locals,
249 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.
256 \subsection{The interpreter stack
257 \label{inspect-stack
}}
259 When the following functions return ``frame records,'' each record
260 is a tuple of six items: the frame object, the filename,
261 the line number of the current line, the function name, a list of
262 lines of context from the source code, and the index of the current
263 line within that list.
264 The optional
\var{context
} argument specifies the number of lines of
265 context to return, which are centered around the current line.
267 \strong{Warning:
} Keeping references to frame objects, as found in
268 the first element of the frame records these functions return, can
269 cause your program to create reference cycles. Once a reference cycle
270 has been created, the lifespan of all objects which can be accessed
271 from the objects which form the cycle can become much longer even if
272 Python's optional cycle detector is enabled. If such cycles must be
273 created, it is important to ensure they are explicitly broken to avoid
274 the delayed destruction of objects and increased memory consumption
277 \begin{funcdesc
}{getouterframes
}{frame
\optional{, context
}}
278 Get a list of frame records for a frame and all higher (calling)
282 \begin{funcdesc
}{getinnerframes
}{traceback
\optional{, context
}}
283 Get a list of frame records for a traceback's frame and all lower
287 \begin{funcdesc
}{currentframe
}{}
288 Return the frame object for the caller's stack frame.
291 \begin{funcdesc
}{stack
}{\optional{context
}}
292 Return a list of frame records for the stack above the caller's
296 \begin{funcdesc
}{trace
}{\optional{context
}}
297 Return a list of frame records for the stack below the current