Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 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 | |
Éric Araujo | 29a0b57 | 2011-08-19 02:14:03 +0200 | [diff] [blame] | 12 | **Source code:** :source:`Lib/inspect.py` |
| 13 | |
| 14 | -------------- |
| 15 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 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 |
Georg Brandl | 3e9d66f | 2008-03-03 20:37:55 +0000 | [diff] [blame] | 34 | class or module. The sixteen functions whose names begin with "is" are mainly |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 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 | +-----------+-----------------+---------------------------+-------+ |
Georg Brandl | 3fbe20c | 2008-03-21 19:20:21 +0000 | [diff] [blame] | 60 | | | im_func or | function object | | |
| 61 | | | __func__ | containing implementation | | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 62 | | | | of method | | |
| 63 | +-----------+-----------------+---------------------------+-------+ |
Georg Brandl | 3fbe20c | 2008-03-21 19:20:21 +0000 | [diff] [blame] | 64 | | | im_self or | instance to which this | | |
| 65 | | | __self__ | method is bound, or | | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 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 | | |
Georg Brandl | 63fa168 | 2007-10-21 10:24:20 +0000 | [diff] [blame] | 75 | | | | :term:`bytecode` | | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 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 | +-----------+-----------------+---------------------------+-------+ |
Facundo Batista | 759bfc6 | 2008-02-18 03:43:43 +0000 | [diff] [blame] | 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 | +-----------+-----------------+---------------------------+-------+ |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 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 | +-----------+-----------------+---------------------------+-------+ |
Georg Brandl | 6f82cd3 | 2010-02-06 18:44:44 +0000 | [diff] [blame] | 132 | | | f_builtins | builtins namespace seen | | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 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 | |
Georg Brandl | 91a4808 | 2008-01-06 15:48:20 +0000 | [diff] [blame] | 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 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 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 |
Georg Brandl | cbb2e49 | 2011-01-09 08:04:37 +0000 | [diff] [blame] | 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. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 248 | |
Georg Brandl | e3c3db5 | 2008-01-11 09:55:53 +0000 | [diff] [blame] | 249 | .. versionchanged:: 2.6 |
| 250 | Returns a :term:`named tuple` ``ModuleInfo(name, suffix, mode, |
| 251 | module_type)``. |
| 252 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 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 | |
Georg Brandl | 78f11ed | 2010-11-26 07:34:20 +0000 | [diff] [blame] | 269 | Return true if the object is a class, whether built-in or created in Python |
| 270 | code. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 271 | |
| 272 | |
| 273 | .. function:: ismethod(object) |
| 274 | |
Georg Brandl | 78f11ed | 2010-11-26 07:34:20 +0000 | [diff] [blame] | 275 | Return true if the object is a bound method written in Python. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 276 | |
| 277 | |
| 278 | .. function:: isfunction(object) |
| 279 | |
Georg Brandl | 78f11ed | 2010-11-26 07:34:20 +0000 | [diff] [blame] | 280 | Return true if the object is a Python function, which includes functions |
| 281 | created by a :term:`lambda` expression. |
| 282 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 283 | |
Facundo Batista | 759bfc6 | 2008-02-18 03:43:43 +0000 | [diff] [blame] | 284 | .. function:: isgeneratorfunction(object) |
| 285 | |
| 286 | Return true if the object is a Python generator function. |
| 287 | |
Andrew M. Kuchling | 3fe1843 | 2008-03-04 01:49:37 +0000 | [diff] [blame] | 288 | .. versionadded:: 2.6 |
| 289 | |
Georg Brandl | 78f11ed | 2010-11-26 07:34:20 +0000 | [diff] [blame] | 290 | |
Facundo Batista | 759bfc6 | 2008-02-18 03:43:43 +0000 | [diff] [blame] | 291 | .. function:: isgenerator(object) |
| 292 | |
| 293 | Return true if the object is a generator. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 294 | |
Andrew M. Kuchling | 3fe1843 | 2008-03-04 01:49:37 +0000 | [diff] [blame] | 295 | .. versionadded:: 2.6 |
| 296 | |
Georg Brandl | 78f11ed | 2010-11-26 07:34:20 +0000 | [diff] [blame] | 297 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 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 | |
Georg Brandl | 78f11ed | 2010-11-26 07:34:20 +0000 | [diff] [blame] | 315 | Return true if the object is a built-in function or a bound built-in method. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 316 | |
| 317 | |
| 318 | .. function:: isroutine(object) |
| 319 | |
| 320 | Return true if the object is a user-defined or built-in function or method. |
| 321 | |
Georg Brandl | 78f11ed | 2010-11-26 07:34:20 +0000 | [diff] [blame] | 322 | |
Christian Heimes | 608c1d8 | 2008-03-03 18:28:04 +0000 | [diff] [blame] | 323 | .. function:: isabstract(object) |
| 324 | |
| 325 | Return true if the object is an abstract base class. |
| 326 | |
| 327 | .. versionadded:: 2.6 |
| 328 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 329 | |
| 330 | .. function:: ismethoddescriptor(object) |
| 331 | |
Georg Brandl | 78f11ed | 2010-11-26 07:34:20 +0000 | [diff] [blame] | 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. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 335 | |
Georg Brandl | 5c174bb | 2007-10-21 10:32:54 +0000 | [diff] [blame] | 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. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 340 | |
Georg Brandl | 5c174bb | 2007-10-21 10:32:54 +0000 | [diff] [blame] | 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`. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 345 | |
| 346 | |
| 347 | .. function:: isdatadescriptor(object) |
| 348 | |
| 349 | Return true if the object is a data descriptor. |
| 350 | |
Georg Brandl | 5c174bb | 2007-10-21 10:32:54 +0000 | [diff] [blame] | 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. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 358 | |
| 359 | .. versionadded:: 2.3 |
| 360 | |
| 361 | |
| 362 | .. function:: isgetsetdescriptor(object) |
| 363 | |
| 364 | Return true if the object is a getset descriptor. |
| 365 | |
Georg Brandl | 6c14e58 | 2009-10-22 11:48:10 +0000 | [diff] [blame] | 366 | .. impl-detail:: |
| 367 | |
| 368 | getsets are attributes defined in extension modules via |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 369 | :c:type:`PyGetSetDef` structures. For Python implementations without such |
Georg Brandl | 6c14e58 | 2009-10-22 11:48:10 +0000 | [diff] [blame] | 370 | types, this method will always return ``False``. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 371 | |
| 372 | .. versionadded:: 2.5 |
| 373 | |
| 374 | |
| 375 | .. function:: ismemberdescriptor(object) |
| 376 | |
| 377 | Return true if the object is a member descriptor. |
| 378 | |
Georg Brandl | 6c14e58 | 2009-10-22 11:48:10 +0000 | [diff] [blame] | 379 | .. impl-detail:: |
| 380 | |
| 381 | Member descriptors are attributes defined in extension modules via |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 382 | :c:type:`PyMemberDef` structures. For Python implementations without such |
Georg Brandl | 6c14e58 | 2009-10-22 11:48:10 +0000 | [diff] [blame] | 383 | types, this method will always return ``False``. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 384 | |
| 385 | .. versionadded:: 2.5 |
| 386 | |
| 387 | |
| 388 | .. _inspect-source: |
| 389 | |
| 390 | Retrieving source code |
| 391 | ---------------------- |
| 392 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 393 | .. function:: getdoc(object) |
| 394 | |
Georg Brandl | 7be19aa | 2008-06-07 15:59:10 +0000 | [diff] [blame] | 395 | Get the documentation string for an object, cleaned up with :func:`cleandoc`. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 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 | |
Georg Brandl | 7be19aa | 2008-06-07 15:59:10 +0000 | [diff] [blame] | 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 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 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 | |
Georg Brandl | cbb2e49 | 2011-01-09 08:04:37 +0000 | [diff] [blame] | 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*. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 477 | |
Georg Brandl | e3c3db5 | 2008-01-11 09:55:53 +0000 | [diff] [blame] | 478 | .. versionchanged:: 2.6 |
| 479 | Returns a :term:`named tuple` ``ArgSpec(args, varargs, keywords, |
| 480 | defaults)``. |
| 481 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 482 | |
| 483 | .. function:: getargvalues(frame) |
| 484 | |
Georg Brandl | cbb2e49 | 2011-01-09 08:04:37 +0000 | [diff] [blame] | 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. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 490 | |
Georg Brandl | e3c3db5 | 2008-01-11 09:55:53 +0000 | [diff] [blame] | 491 | .. versionchanged:: 2.6 |
| 492 | Returns a :term:`named tuple` ``ArgInfo(args, varargs, keywords, |
| 493 | locals)``. |
| 494 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 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 | |
Benjamin Peterson | 7e21325 | 2010-03-30 17:58:13 +0000 | [diff] [blame] | 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 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 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 | |
Georg Brandl | 16a57f6 | 2009-04-27 15:29:09 +0000 | [diff] [blame] | 554 | .. note:: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 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 | |
Georg Brandl | e3c3db5 | 2008-01-11 09:55:53 +0000 | [diff] [blame] | 586 | .. versionchanged:: 2.6 |
| 587 | Returns a :term:`named tuple` ``Traceback(filename, lineno, function, |
| 588 | code_context, index)``. |
| 589 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 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 | |
Georg Brandl | 6c14e58 | 2009-10-22 11:48:10 +0000 | [diff] [blame] | 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``. |
Michael Foord | 668be58 | 2009-09-13 16:46:19 +0000 | [diff] [blame] | 617 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 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 | |