[2] | 1 | :mod:`inspect` --- Inspect live objects
|
---|
| 2 | =======================================
|
---|
| 3 |
|
---|
| 4 | .. module:: inspect
|
---|
| 5 | :synopsis: Extract information and source code from live objects.
|
---|
| 6 | .. moduleauthor:: Ka-Ping Yee <ping@lfw.org>
|
---|
| 7 | .. sectionauthor:: Ka-Ping Yee <ping@lfw.org>
|
---|
| 8 |
|
---|
| 9 |
|
---|
| 10 | .. versionadded:: 2.1
|
---|
| 11 |
|
---|
[391] | 12 | **Source code:** :source:`Lib/inspect.py`
|
---|
| 13 |
|
---|
| 14 | --------------
|
---|
| 15 |
|
---|
[2] | 16 | The :mod:`inspect` module provides several useful functions to help get
|
---|
| 17 | information about live objects such as modules, classes, methods, functions,
|
---|
| 18 | tracebacks, frame objects, and code objects. For example, it can help you
|
---|
| 19 | examine the contents of a class, retrieve the source code of a method, extract
|
---|
| 20 | and format the argument list for a function, or get all the information you need
|
---|
| 21 | to display a detailed traceback.
|
---|
| 22 |
|
---|
| 23 | There are four main kinds of services provided by this module: type checking,
|
---|
| 24 | getting source code, inspecting classes and functions, and examining the
|
---|
| 25 | interpreter stack.
|
---|
| 26 |
|
---|
| 27 |
|
---|
| 28 | .. _inspect-types:
|
---|
| 29 |
|
---|
| 30 | Types and members
|
---|
| 31 | -----------------
|
---|
| 32 |
|
---|
| 33 | The :func:`getmembers` function retrieves the members of an object such as a
|
---|
| 34 | class or module. The sixteen functions whose names begin with "is" are mainly
|
---|
| 35 | provided as convenient choices for the second argument to :func:`getmembers`.
|
---|
| 36 | They also help you determine when you can expect to find the following special
|
---|
| 37 | attributes:
|
---|
| 38 |
|
---|
| 39 | +-----------+-----------------+---------------------------+-------+
|
---|
| 40 | | Type | Attribute | Description | Notes |
|
---|
| 41 | +===========+=================+===========================+=======+
|
---|
| 42 | | module | __doc__ | documentation string | |
|
---|
| 43 | +-----------+-----------------+---------------------------+-------+
|
---|
| 44 | | | __file__ | filename (missing for | |
|
---|
| 45 | | | | built-in modules) | |
|
---|
| 46 | +-----------+-----------------+---------------------------+-------+
|
---|
| 47 | | class | __doc__ | documentation string | |
|
---|
| 48 | +-----------+-----------------+---------------------------+-------+
|
---|
| 49 | | | __module__ | name of module in which | |
|
---|
| 50 | | | | this class was defined | |
|
---|
| 51 | +-----------+-----------------+---------------------------+-------+
|
---|
| 52 | | method | __doc__ | documentation string | |
|
---|
| 53 | +-----------+-----------------+---------------------------+-------+
|
---|
| 54 | | | __name__ | name with which this | |
|
---|
| 55 | | | | method was defined | |
|
---|
| 56 | +-----------+-----------------+---------------------------+-------+
|
---|
| 57 | | | im_class | class object that asked | \(1) |
|
---|
| 58 | | | | for this method | |
|
---|
| 59 | +-----------+-----------------+---------------------------+-------+
|
---|
| 60 | | | im_func or | function object | |
|
---|
| 61 | | | __func__ | containing implementation | |
|
---|
| 62 | | | | of method | |
|
---|
| 63 | +-----------+-----------------+---------------------------+-------+
|
---|
| 64 | | | im_self or | instance to which this | |
|
---|
| 65 | | | __self__ | method is bound, or | |
|
---|
| 66 | | | | ``None`` | |
|
---|
| 67 | +-----------+-----------------+---------------------------+-------+
|
---|
| 68 | | function | __doc__ | documentation string | |
|
---|
| 69 | +-----------+-----------------+---------------------------+-------+
|
---|
| 70 | | | __name__ | name with which this | |
|
---|
| 71 | | | | function was defined | |
|
---|
| 72 | +-----------+-----------------+---------------------------+-------+
|
---|
| 73 | | | func_code | code object containing | |
|
---|
| 74 | | | | compiled function | |
|
---|
| 75 | | | | :term:`bytecode` | |
|
---|
| 76 | +-----------+-----------------+---------------------------+-------+
|
---|
| 77 | | | func_defaults | tuple of any default | |
|
---|
| 78 | | | | values for arguments | |
|
---|
| 79 | +-----------+-----------------+---------------------------+-------+
|
---|
| 80 | | | func_doc | (same as __doc__) | |
|
---|
| 81 | +-----------+-----------------+---------------------------+-------+
|
---|
| 82 | | | func_globals | global namespace in which | |
|
---|
| 83 | | | | this function was defined | |
|
---|
| 84 | +-----------+-----------------+---------------------------+-------+
|
---|
| 85 | | | func_name | (same as __name__) | |
|
---|
| 86 | +-----------+-----------------+---------------------------+-------+
|
---|
| 87 | | generator | __iter__ | defined to support | |
|
---|
| 88 | | | | iteration over container | |
|
---|
| 89 | +-----------+-----------------+---------------------------+-------+
|
---|
| 90 | | | close | raises new GeneratorExit | |
|
---|
| 91 | | | | exception inside the | |
|
---|
| 92 | | | | generator to terminate | |
|
---|
| 93 | | | | the iteration | |
|
---|
| 94 | +-----------+-----------------+---------------------------+-------+
|
---|
| 95 | | | gi_code | code object | |
|
---|
| 96 | +-----------+-----------------+---------------------------+-------+
|
---|
| 97 | | | gi_frame | frame object or possibly | |
|
---|
| 98 | | | | None once the generator | |
|
---|
| 99 | | | | has been exhausted | |
|
---|
| 100 | +-----------+-----------------+---------------------------+-------+
|
---|
| 101 | | | gi_running | set to 1 when generator | |
|
---|
| 102 | | | | is executing, 0 otherwise | |
|
---|
| 103 | +-----------+-----------------+---------------------------+-------+
|
---|
| 104 | | | next | return the next item from | |
|
---|
| 105 | | | | the container | |
|
---|
| 106 | +-----------+-----------------+---------------------------+-------+
|
---|
| 107 | | | send | resumes the generator and | |
|
---|
| 108 | | | | "sends" a value that | |
|
---|
| 109 | | | | becomes the result of the | |
|
---|
| 110 | | | | current yield-expression | |
|
---|
| 111 | +-----------+-----------------+---------------------------+-------+
|
---|
| 112 | | | throw | used to raise an | |
|
---|
| 113 | | | | exception inside the | |
|
---|
| 114 | | | | generator | |
|
---|
| 115 | +-----------+-----------------+---------------------------+-------+
|
---|
| 116 | | traceback | tb_frame | frame object at this | |
|
---|
| 117 | | | | level | |
|
---|
| 118 | +-----------+-----------------+---------------------------+-------+
|
---|
| 119 | | | tb_lasti | index of last attempted | |
|
---|
| 120 | | | | instruction in bytecode | |
|
---|
| 121 | +-----------+-----------------+---------------------------+-------+
|
---|
| 122 | | | tb_lineno | current line number in | |
|
---|
| 123 | | | | Python source code | |
|
---|
| 124 | +-----------+-----------------+---------------------------+-------+
|
---|
| 125 | | | tb_next | next inner traceback | |
|
---|
| 126 | | | | object (called by this | |
|
---|
| 127 | | | | level) | |
|
---|
| 128 | +-----------+-----------------+---------------------------+-------+
|
---|
| 129 | | frame | f_back | next outer frame object | |
|
---|
| 130 | | | | (this frame's caller) | |
|
---|
| 131 | +-----------+-----------------+---------------------------+-------+
|
---|
| 132 | | | f_builtins | builtins namespace seen | |
|
---|
| 133 | | | | by this frame | |
|
---|
| 134 | +-----------+-----------------+---------------------------+-------+
|
---|
| 135 | | | f_code | code object being | |
|
---|
| 136 | | | | executed in this frame | |
|
---|
| 137 | +-----------+-----------------+---------------------------+-------+
|
---|
| 138 | | | f_exc_traceback | traceback if raised in | |
|
---|
| 139 | | | | this frame, or ``None`` | |
|
---|
| 140 | +-----------+-----------------+---------------------------+-------+
|
---|
| 141 | | | f_exc_type | exception type if raised | |
|
---|
| 142 | | | | in this frame, or | |
|
---|
| 143 | | | | ``None`` | |
|
---|
| 144 | +-----------+-----------------+---------------------------+-------+
|
---|
| 145 | | | f_exc_value | exception value if raised | |
|
---|
| 146 | | | | in this frame, or | |
|
---|
| 147 | | | | ``None`` | |
|
---|
| 148 | +-----------+-----------------+---------------------------+-------+
|
---|
| 149 | | | f_globals | global namespace seen by | |
|
---|
| 150 | | | | this frame | |
|
---|
| 151 | +-----------+-----------------+---------------------------+-------+
|
---|
| 152 | | | f_lasti | index of last attempted | |
|
---|
| 153 | | | | instruction in bytecode | |
|
---|
| 154 | +-----------+-----------------+---------------------------+-------+
|
---|
| 155 | | | f_lineno | current line number in | |
|
---|
| 156 | | | | Python source code | |
|
---|
| 157 | +-----------+-----------------+---------------------------+-------+
|
---|
| 158 | | | f_locals | local namespace seen by | |
|
---|
| 159 | | | | this frame | |
|
---|
| 160 | +-----------+-----------------+---------------------------+-------+
|
---|
| 161 | | | f_restricted | 0 or 1 if frame is in | |
|
---|
| 162 | | | | restricted execution mode | |
|
---|
| 163 | +-----------+-----------------+---------------------------+-------+
|
---|
| 164 | | | f_trace | tracing function for this | |
|
---|
| 165 | | | | frame, or ``None`` | |
|
---|
| 166 | +-----------+-----------------+---------------------------+-------+
|
---|
| 167 | | code | co_argcount | number of arguments (not | |
|
---|
| 168 | | | | including \* or \*\* | |
|
---|
| 169 | | | | args) | |
|
---|
| 170 | +-----------+-----------------+---------------------------+-------+
|
---|
| 171 | | | co_code | string of raw compiled | |
|
---|
| 172 | | | | bytecode | |
|
---|
| 173 | +-----------+-----------------+---------------------------+-------+
|
---|
| 174 | | | co_consts | tuple of constants used | |
|
---|
| 175 | | | | in the bytecode | |
|
---|
| 176 | +-----------+-----------------+---------------------------+-------+
|
---|
| 177 | | | co_filename | name of file in which | |
|
---|
| 178 | | | | this code object was | |
|
---|
| 179 | | | | created | |
|
---|
| 180 | +-----------+-----------------+---------------------------+-------+
|
---|
| 181 | | | co_firstlineno | number of first line in | |
|
---|
| 182 | | | | Python source code | |
|
---|
| 183 | +-----------+-----------------+---------------------------+-------+
|
---|
| 184 | | | co_flags | bitmap: 1=optimized ``|`` | |
|
---|
| 185 | | | | 2=newlocals ``|`` 4=\*arg | |
|
---|
| 186 | | | | ``|`` 8=\*\*arg | |
|
---|
| 187 | +-----------+-----------------+---------------------------+-------+
|
---|
| 188 | | | co_lnotab | encoded mapping of line | |
|
---|
| 189 | | | | numbers to bytecode | |
|
---|
| 190 | | | | indices | |
|
---|
| 191 | +-----------+-----------------+---------------------------+-------+
|
---|
| 192 | | | co_name | name with which this code | |
|
---|
| 193 | | | | object was defined | |
|
---|
| 194 | +-----------+-----------------+---------------------------+-------+
|
---|
| 195 | | | co_names | tuple of names of local | |
|
---|
| 196 | | | | variables | |
|
---|
| 197 | +-----------+-----------------+---------------------------+-------+
|
---|
| 198 | | | co_nlocals | number of local variables | |
|
---|
| 199 | +-----------+-----------------+---------------------------+-------+
|
---|
| 200 | | | co_stacksize | virtual machine stack | |
|
---|
| 201 | | | | space required | |
|
---|
| 202 | +-----------+-----------------+---------------------------+-------+
|
---|
| 203 | | | co_varnames | tuple of names of | |
|
---|
| 204 | | | | arguments and local | |
|
---|
| 205 | | | | variables | |
|
---|
| 206 | +-----------+-----------------+---------------------------+-------+
|
---|
| 207 | | builtin | __doc__ | documentation string | |
|
---|
| 208 | +-----------+-----------------+---------------------------+-------+
|
---|
| 209 | | | __name__ | original name of this | |
|
---|
| 210 | | | | function or method | |
|
---|
| 211 | +-----------+-----------------+---------------------------+-------+
|
---|
| 212 | | | __self__ | instance to which a | |
|
---|
| 213 | | | | method is bound, or | |
|
---|
| 214 | | | | ``None`` | |
|
---|
| 215 | +-----------+-----------------+---------------------------+-------+
|
---|
| 216 |
|
---|
| 217 | Note:
|
---|
| 218 |
|
---|
| 219 | (1)
|
---|
| 220 | .. versionchanged:: 2.2
|
---|
| 221 | :attr:`im_class` used to refer to the class that defined the method.
|
---|
| 222 |
|
---|
| 223 |
|
---|
| 224 | .. function:: getmembers(object[, predicate])
|
---|
| 225 |
|
---|
| 226 | Return all the members of an object in a list of (name, value) pairs sorted by
|
---|
| 227 | name. If the optional *predicate* argument is supplied, only members for which
|
---|
| 228 | the predicate returns a true value are included.
|
---|
| 229 |
|
---|
| 230 | .. note::
|
---|
| 231 |
|
---|
| 232 | :func:`getmembers` does not return metaclass attributes when the argument
|
---|
| 233 | is a class (this behavior is inherited from the :func:`dir` function).
|
---|
| 234 |
|
---|
| 235 |
|
---|
| 236 | .. function:: getmoduleinfo(path)
|
---|
| 237 |
|
---|
| 238 | Return a tuple of values that describe how Python will interpret the file
|
---|
| 239 | identified by *path* if it is a module, or ``None`` if it would not be
|
---|
[391] | 240 | identified as a module. The return tuple is ``(name, suffix, mode,
|
---|
| 241 | module_type)``, where *name* is the name of the module without the name of
|
---|
| 242 | any enclosing package, *suffix* is the trailing part of the file name (which
|
---|
| 243 | may not be a dot-delimited extension), *mode* is the :func:`open` mode that
|
---|
| 244 | would be used (``'r'`` or ``'rb'``), and *module_type* is an integer giving
|
---|
| 245 | the type of the module. *module_type* will have a value which can be
|
---|
| 246 | compared to the constants defined in the :mod:`imp` module; see the
|
---|
| 247 | documentation for that module for more information on module types.
|
---|
[2] | 248 |
|
---|
| 249 | .. versionchanged:: 2.6
|
---|
| 250 | Returns a :term:`named tuple` ``ModuleInfo(name, suffix, mode,
|
---|
| 251 | module_type)``.
|
---|
| 252 |
|
---|
| 253 |
|
---|
| 254 | .. function:: getmodulename(path)
|
---|
| 255 |
|
---|
| 256 | Return the name of the module named by the file *path*, without including the
|
---|
| 257 | names of enclosing packages. This uses the same algorithm as the interpreter
|
---|
| 258 | uses when searching for modules. If the name cannot be matched according to the
|
---|
| 259 | interpreter's rules, ``None`` is returned.
|
---|
| 260 |
|
---|
| 261 |
|
---|
| 262 | .. function:: ismodule(object)
|
---|
| 263 |
|
---|
| 264 | Return true if the object is a module.
|
---|
| 265 |
|
---|
| 266 |
|
---|
| 267 | .. function:: isclass(object)
|
---|
| 268 |
|
---|
[391] | 269 | Return true if the object is a class, whether built-in or created in Python
|
---|
| 270 | code.
|
---|
[2] | 271 |
|
---|
| 272 |
|
---|
| 273 | .. function:: ismethod(object)
|
---|
| 274 |
|
---|
[391] | 275 | Return true if the object is a bound method written in Python.
|
---|
[2] | 276 |
|
---|
| 277 |
|
---|
| 278 | .. function:: isfunction(object)
|
---|
| 279 |
|
---|
[391] | 280 | Return true if the object is a Python function, which includes functions
|
---|
| 281 | created by a :term:`lambda` expression.
|
---|
[2] | 282 |
|
---|
[391] | 283 |
|
---|
[2] | 284 | .. function:: isgeneratorfunction(object)
|
---|
| 285 |
|
---|
| 286 | Return true if the object is a Python generator function.
|
---|
| 287 |
|
---|
| 288 | .. versionadded:: 2.6
|
---|
| 289 |
|
---|
[391] | 290 |
|
---|
[2] | 291 | .. function:: isgenerator(object)
|
---|
| 292 |
|
---|
| 293 | Return true if the object is a generator.
|
---|
| 294 |
|
---|
| 295 | .. versionadded:: 2.6
|
---|
| 296 |
|
---|
[391] | 297 |
|
---|
[2] | 298 | .. function:: istraceback(object)
|
---|
| 299 |
|
---|
| 300 | Return true if the object is a traceback.
|
---|
| 301 |
|
---|
| 302 |
|
---|
| 303 | .. function:: isframe(object)
|
---|
| 304 |
|
---|
| 305 | Return true if the object is a frame.
|
---|
| 306 |
|
---|
| 307 |
|
---|
| 308 | .. function:: iscode(object)
|
---|
| 309 |
|
---|
| 310 | Return true if the object is a code.
|
---|
| 311 |
|
---|
| 312 |
|
---|
| 313 | .. function:: isbuiltin(object)
|
---|
| 314 |
|
---|
[391] | 315 | Return true if the object is a built-in function or a bound built-in method.
|
---|
[2] | 316 |
|
---|
| 317 |
|
---|
| 318 | .. function:: isroutine(object)
|
---|
| 319 |
|
---|
| 320 | Return true if the object is a user-defined or built-in function or method.
|
---|
| 321 |
|
---|
[391] | 322 |
|
---|
[2] | 323 | .. function:: isabstract(object)
|
---|
| 324 |
|
---|
| 325 | Return true if the object is an abstract base class.
|
---|
| 326 |
|
---|
| 327 | .. versionadded:: 2.6
|
---|
| 328 |
|
---|
| 329 |
|
---|
| 330 | .. function:: ismethoddescriptor(object)
|
---|
| 331 |
|
---|
[391] | 332 | Return true if the object is a method descriptor, but not if
|
---|
| 333 | :func:`ismethod`, :func:`isclass`, :func:`isfunction` or :func:`isbuiltin`
|
---|
| 334 | are true.
|
---|
[2] | 335 |
|
---|
| 336 | This is new as of Python 2.2, and, for example, is true of
|
---|
| 337 | ``int.__add__``. An object passing this test has a :attr:`__get__` attribute
|
---|
| 338 | but not a :attr:`__set__` attribute, but beyond that the set of attributes
|
---|
| 339 | varies. :attr:`__name__` is usually sensible, and :attr:`__doc__` often is.
|
---|
| 340 |
|
---|
| 341 | Methods implemented via descriptors that also pass one of the other tests
|
---|
| 342 | return false from the :func:`ismethoddescriptor` test, simply because the
|
---|
| 343 | other tests promise more -- you can, e.g., count on having the
|
---|
| 344 | :attr:`im_func` attribute (etc) when an object passes :func:`ismethod`.
|
---|
| 345 |
|
---|
| 346 |
|
---|
| 347 | .. function:: isdatadescriptor(object)
|
---|
| 348 |
|
---|
| 349 | Return true if the object is a data descriptor.
|
---|
| 350 |
|
---|
| 351 | Data descriptors have both a :attr:`__get__` and a :attr:`__set__` attribute.
|
---|
| 352 | Examples are properties (defined in Python), getsets, and members. The
|
---|
| 353 | latter two are defined in C and there are more specific tests available for
|
---|
| 354 | those types, which is robust across Python implementations. Typically, data
|
---|
| 355 | descriptors will also have :attr:`__name__` and :attr:`__doc__` attributes
|
---|
| 356 | (properties, getsets, and members have both of these attributes), but this is
|
---|
| 357 | not guaranteed.
|
---|
| 358 |
|
---|
| 359 | .. versionadded:: 2.3
|
---|
| 360 |
|
---|
| 361 |
|
---|
| 362 | .. function:: isgetsetdescriptor(object)
|
---|
| 363 |
|
---|
| 364 | Return true if the object is a getset descriptor.
|
---|
| 365 |
|
---|
| 366 | .. impl-detail::
|
---|
| 367 |
|
---|
| 368 | getsets are attributes defined in extension modules via
|
---|
[391] | 369 | :c:type:`PyGetSetDef` structures. For Python implementations without such
|
---|
[2] | 370 | types, this method will always return ``False``.
|
---|
| 371 |
|
---|
| 372 | .. versionadded:: 2.5
|
---|
| 373 |
|
---|
| 374 |
|
---|
| 375 | .. function:: ismemberdescriptor(object)
|
---|
| 376 |
|
---|
| 377 | Return true if the object is a member descriptor.
|
---|
| 378 |
|
---|
| 379 | .. impl-detail::
|
---|
| 380 |
|
---|
| 381 | Member descriptors are attributes defined in extension modules via
|
---|
[391] | 382 | :c:type:`PyMemberDef` structures. For Python implementations without such
|
---|
[2] | 383 | types, this method will always return ``False``.
|
---|
| 384 |
|
---|
| 385 | .. versionadded:: 2.5
|
---|
| 386 |
|
---|
| 387 |
|
---|
| 388 | .. _inspect-source:
|
---|
| 389 |
|
---|
| 390 | Retrieving source code
|
---|
| 391 | ----------------------
|
---|
| 392 |
|
---|
| 393 | .. function:: getdoc(object)
|
---|
| 394 |
|
---|
| 395 | Get the documentation string for an object, cleaned up with :func:`cleandoc`.
|
---|
| 396 |
|
---|
| 397 |
|
---|
| 398 | .. function:: getcomments(object)
|
---|
| 399 |
|
---|
| 400 | Return in a single string any lines of comments immediately preceding the
|
---|
| 401 | object's source code (for a class, function, or method), or at the top of the
|
---|
| 402 | Python source file (if the object is a module).
|
---|
| 403 |
|
---|
| 404 |
|
---|
| 405 | .. function:: getfile(object)
|
---|
| 406 |
|
---|
| 407 | Return the name of the (text or binary) file in which an object was defined.
|
---|
| 408 | This will fail with a :exc:`TypeError` if the object is a built-in module,
|
---|
| 409 | class, or function.
|
---|
| 410 |
|
---|
| 411 |
|
---|
| 412 | .. function:: getmodule(object)
|
---|
| 413 |
|
---|
| 414 | Try to guess which module an object was defined in.
|
---|
| 415 |
|
---|
| 416 |
|
---|
| 417 | .. function:: getsourcefile(object)
|
---|
| 418 |
|
---|
| 419 | Return the name of the Python source file in which an object was defined. This
|
---|
| 420 | will fail with a :exc:`TypeError` if the object is a built-in module, class, or
|
---|
| 421 | function.
|
---|
| 422 |
|
---|
| 423 |
|
---|
| 424 | .. function:: getsourcelines(object)
|
---|
| 425 |
|
---|
| 426 | Return a list of source lines and starting line number for an object. The
|
---|
| 427 | argument may be a module, class, method, function, traceback, frame, or code
|
---|
| 428 | object. The source code is returned as a list of the lines corresponding to the
|
---|
| 429 | object and the line number indicates where in the original source file the first
|
---|
| 430 | line of code was found. An :exc:`IOError` is raised if the source code cannot
|
---|
| 431 | be retrieved.
|
---|
| 432 |
|
---|
| 433 |
|
---|
| 434 | .. function:: getsource(object)
|
---|
| 435 |
|
---|
| 436 | Return the text of the source code for an object. The argument may be a module,
|
---|
| 437 | class, method, function, traceback, frame, or code object. The source code is
|
---|
| 438 | returned as a single string. An :exc:`IOError` is raised if the source code
|
---|
| 439 | cannot be retrieved.
|
---|
| 440 |
|
---|
| 441 |
|
---|
| 442 | .. function:: cleandoc(doc)
|
---|
| 443 |
|
---|
| 444 | Clean up indentation from docstrings that are indented to line up with blocks
|
---|
| 445 | of code. Any whitespace that can be uniformly removed from the second line
|
---|
| 446 | onwards is removed. Also, all tabs are expanded to spaces.
|
---|
| 447 |
|
---|
| 448 | .. versionadded:: 2.6
|
---|
| 449 |
|
---|
| 450 |
|
---|
| 451 | .. _inspect-classes-functions:
|
---|
| 452 |
|
---|
| 453 | Classes and functions
|
---|
| 454 | ---------------------
|
---|
| 455 |
|
---|
| 456 |
|
---|
| 457 | .. function:: getclasstree(classes[, unique])
|
---|
| 458 |
|
---|
| 459 | Arrange the given list of classes into a hierarchy of nested lists. Where a
|
---|
| 460 | nested list appears, it contains classes derived from the class whose entry
|
---|
| 461 | immediately precedes the list. Each entry is a 2-tuple containing a class and a
|
---|
| 462 | tuple of its base classes. If the *unique* argument is true, exactly one entry
|
---|
| 463 | appears in the returned structure for each class in the given list. Otherwise,
|
---|
| 464 | classes using multiple inheritance and their descendants will appear multiple
|
---|
| 465 | times.
|
---|
| 466 |
|
---|
| 467 |
|
---|
| 468 | .. function:: getargspec(func)
|
---|
| 469 |
|
---|
[391] | 470 | Get the names and default values of a Python function's arguments. A tuple of
|
---|
| 471 | four things is returned: ``(args, varargs, keywords, defaults)``. *args* is a
|
---|
| 472 | list of the argument names (it may contain nested lists). *varargs* and
|
---|
| 473 | *keywords* are the names of the ``*`` and ``**`` arguments or
|
---|
| 474 | ``None``. *defaults* is a tuple of default argument values or None if there
|
---|
| 475 | are no default arguments; if this tuple has *n* elements, they correspond to
|
---|
| 476 | the last *n* elements listed in *args*.
|
---|
[2] | 477 |
|
---|
| 478 | .. versionchanged:: 2.6
|
---|
| 479 | Returns a :term:`named tuple` ``ArgSpec(args, varargs, keywords,
|
---|
| 480 | defaults)``.
|
---|
| 481 |
|
---|
| 482 |
|
---|
| 483 | .. function:: getargvalues(frame)
|
---|
| 484 |
|
---|
[391] | 485 | Get information about arguments passed into a particular frame. A tuple of
|
---|
| 486 | four things is returned: ``(args, varargs, keywords, locals)``. *args* is a
|
---|
| 487 | list of the argument names (it may contain nested lists). *varargs* and
|
---|
| 488 | *keywords* are the names of the ``*`` and ``**`` arguments or ``None``.
|
---|
| 489 | *locals* is the locals dictionary of the given frame.
|
---|
[2] | 490 |
|
---|
| 491 | .. versionchanged:: 2.6
|
---|
| 492 | Returns a :term:`named tuple` ``ArgInfo(args, varargs, keywords,
|
---|
| 493 | locals)``.
|
---|
| 494 |
|
---|
| 495 |
|
---|
| 496 | .. function:: formatargspec(args[, varargs, varkw, defaults, formatarg, formatvarargs, formatvarkw, formatvalue, join])
|
---|
| 497 |
|
---|
| 498 | Format a pretty argument spec from the four values returned by
|
---|
| 499 | :func:`getargspec`. The format\* arguments are the corresponding optional
|
---|
| 500 | formatting functions that are called to turn names and values into strings.
|
---|
| 501 |
|
---|
| 502 |
|
---|
| 503 | .. function:: formatargvalues(args[, varargs, varkw, locals, formatarg, formatvarargs, formatvarkw, formatvalue, join])
|
---|
| 504 |
|
---|
| 505 | Format a pretty argument spec from the four values returned by
|
---|
| 506 | :func:`getargvalues`. The format\* arguments are the corresponding optional
|
---|
| 507 | formatting functions that are called to turn names and values into strings.
|
---|
| 508 |
|
---|
| 509 |
|
---|
| 510 | .. function:: getmro(cls)
|
---|
| 511 |
|
---|
| 512 | Return a tuple of class cls's base classes, including cls, in method resolution
|
---|
| 513 | order. No class appears more than once in this tuple. Note that the method
|
---|
| 514 | resolution order depends on cls's type. Unless a very peculiar user-defined
|
---|
| 515 | metatype is in use, cls will be the first element of the tuple.
|
---|
| 516 |
|
---|
| 517 |
|
---|
[391] | 518 | .. function:: getcallargs(func[, *args][, **kwds])
|
---|
| 519 |
|
---|
| 520 | Bind the *args* and *kwds* to the argument names of the Python function or
|
---|
| 521 | method *func*, as if it was called with them. For bound methods, bind also the
|
---|
| 522 | first argument (typically named ``self``) to the associated instance. A dict
|
---|
| 523 | is returned, mapping the argument names (including the names of the ``*`` and
|
---|
| 524 | ``**`` arguments, if any) to their values from *args* and *kwds*. In case of
|
---|
| 525 | invoking *func* incorrectly, i.e. whenever ``func(*args, **kwds)`` would raise
|
---|
| 526 | an exception because of incompatible signature, an exception of the same type
|
---|
| 527 | and the same or similar message is raised. For example::
|
---|
| 528 |
|
---|
| 529 | >>> from inspect import getcallargs
|
---|
| 530 | >>> def f(a, b=1, *pos, **named):
|
---|
| 531 | ... pass
|
---|
| 532 | >>> getcallargs(f, 1, 2, 3)
|
---|
| 533 | {'a': 1, 'named': {}, 'b': 2, 'pos': (3,)}
|
---|
| 534 | >>> getcallargs(f, a=2, x=4)
|
---|
| 535 | {'a': 2, 'named': {'x': 4}, 'b': 1, 'pos': ()}
|
---|
| 536 | >>> getcallargs(f)
|
---|
| 537 | Traceback (most recent call last):
|
---|
| 538 | ...
|
---|
| 539 | TypeError: f() takes at least 1 argument (0 given)
|
---|
| 540 |
|
---|
| 541 | .. versionadded:: 2.7
|
---|
| 542 |
|
---|
| 543 |
|
---|
[2] | 544 | .. _inspect-stack:
|
---|
| 545 |
|
---|
| 546 | The interpreter stack
|
---|
| 547 | ---------------------
|
---|
| 548 |
|
---|
| 549 | When the following functions return "frame records," each record is a tuple of
|
---|
| 550 | six items: the frame object, the filename, the line number of the current line,
|
---|
| 551 | the function name, a list of lines of context from the source code, and the
|
---|
| 552 | index of the current line within that list.
|
---|
| 553 |
|
---|
| 554 | .. note::
|
---|
| 555 |
|
---|
| 556 | Keeping references to frame objects, as found in the first element of the frame
|
---|
| 557 | records these functions return, can cause your program to create reference
|
---|
| 558 | cycles. Once a reference cycle has been created, the lifespan of all objects
|
---|
| 559 | which can be accessed from the objects which form the cycle can become much
|
---|
| 560 | longer even if Python's optional cycle detector is enabled. If such cycles must
|
---|
| 561 | be created, it is important to ensure they are explicitly broken to avoid the
|
---|
| 562 | delayed destruction of objects and increased memory consumption which occurs.
|
---|
| 563 |
|
---|
| 564 | Though the cycle detector will catch these, destruction of the frames (and local
|
---|
| 565 | variables) can be made deterministic by removing the cycle in a
|
---|
| 566 | :keyword:`finally` clause. This is also important if the cycle detector was
|
---|
| 567 | disabled when Python was compiled or using :func:`gc.disable`. For example::
|
---|
| 568 |
|
---|
| 569 | def handle_stackframe_without_leak():
|
---|
| 570 | frame = inspect.currentframe()
|
---|
| 571 | try:
|
---|
| 572 | # do something with the frame
|
---|
| 573 | finally:
|
---|
| 574 | del frame
|
---|
| 575 |
|
---|
| 576 | The optional *context* argument supported by most of these functions specifies
|
---|
| 577 | the number of lines of context to return, which are centered around the current
|
---|
| 578 | line.
|
---|
| 579 |
|
---|
| 580 |
|
---|
| 581 | .. function:: getframeinfo(frame[, context])
|
---|
| 582 |
|
---|
| 583 | Get information about a frame or traceback object. A 5-tuple is returned, the
|
---|
| 584 | last five elements of the frame's frame record.
|
---|
| 585 |
|
---|
| 586 | .. versionchanged:: 2.6
|
---|
| 587 | Returns a :term:`named tuple` ``Traceback(filename, lineno, function,
|
---|
| 588 | code_context, index)``.
|
---|
| 589 |
|
---|
| 590 |
|
---|
| 591 | .. function:: getouterframes(frame[, context])
|
---|
| 592 |
|
---|
| 593 | Get a list of frame records for a frame and all outer frames. These frames
|
---|
| 594 | represent the calls that lead to the creation of *frame*. The first entry in the
|
---|
| 595 | returned list represents *frame*; the last entry represents the outermost call
|
---|
| 596 | on *frame*'s stack.
|
---|
| 597 |
|
---|
| 598 |
|
---|
| 599 | .. function:: getinnerframes(traceback[, context])
|
---|
| 600 |
|
---|
| 601 | Get a list of frame records for a traceback's frame and all inner frames. These
|
---|
| 602 | frames represent calls made as a consequence of *frame*. The first entry in the
|
---|
| 603 | list represents *traceback*; the last entry represents where the exception was
|
---|
| 604 | raised.
|
---|
| 605 |
|
---|
| 606 |
|
---|
| 607 | .. function:: currentframe()
|
---|
| 608 |
|
---|
| 609 | Return the frame object for the caller's stack frame.
|
---|
| 610 |
|
---|
| 611 | .. impl-detail::
|
---|
| 612 |
|
---|
| 613 | This function relies on Python stack frame support in the interpreter,
|
---|
| 614 | which isn't guaranteed to exist in all implementations of Python. If
|
---|
| 615 | running in an implementation without Python stack frame support this
|
---|
| 616 | function returns ``None``.
|
---|
| 617 |
|
---|
| 618 |
|
---|
| 619 | .. function:: stack([context])
|
---|
| 620 |
|
---|
| 621 | Return a list of frame records for the caller's stack. The first entry in the
|
---|
| 622 | returned list represents the caller; the last entry represents the outermost
|
---|
| 623 | call on the stack.
|
---|
| 624 |
|
---|
| 625 |
|
---|
| 626 | .. function:: trace([context])
|
---|
| 627 |
|
---|
| 628 | Return a list of frame records for the stack between the current frame and the
|
---|
| 629 | frame in which an exception currently being handled was raised in. The first
|
---|
| 630 | entry in the list represents the caller; the last entry represents where the
|
---|
| 631 | exception was raised.
|
---|
| 632 |
|
---|