Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | :mod:`inspect` --- Inspect live objects |
| 2 | ======================================= |
| 3 | |
| 4 | .. module:: inspect |
| 5 | :synopsis: Extract information and source code from live objects. |
Terry Jan Reedy | fa089b9 | 2016-06-11 15:02:54 -0400 | [diff] [blame] | 6 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 7 | .. moduleauthor:: Ka-Ping Yee <ping@lfw.org> |
| 8 | .. sectionauthor:: Ka-Ping Yee <ping@lfw.org> |
| 9 | |
Raymond Hettinger | 469271d | 2011-01-27 20:38:46 +0000 | [diff] [blame] | 10 | **Source code:** :source:`Lib/inspect.py` |
| 11 | |
| 12 | -------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 13 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 14 | The :mod:`inspect` module provides several useful functions to help get |
| 15 | information about live objects such as modules, classes, methods, functions, |
| 16 | tracebacks, frame objects, and code objects. For example, it can help you |
| 17 | examine the contents of a class, retrieve the source code of a method, extract |
| 18 | and format the argument list for a function, or get all the information you need |
| 19 | to display a detailed traceback. |
| 20 | |
| 21 | There are four main kinds of services provided by this module: type checking, |
| 22 | getting source code, inspecting classes and functions, and examining the |
| 23 | interpreter stack. |
| 24 | |
| 25 | |
| 26 | .. _inspect-types: |
| 27 | |
| 28 | Types and members |
| 29 | ----------------- |
| 30 | |
| 31 | The :func:`getmembers` function retrieves the members of an object such as a |
Yury Selivanov | 59a3b67 | 2015-06-30 22:06:42 -0400 | [diff] [blame] | 32 | class or module. The functions whose names begin with "is" are mainly |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 33 | provided as convenient choices for the second argument to :func:`getmembers`. |
| 34 | They also help you determine when you can expect to find the following special |
| 35 | attributes: |
| 36 | |
Nathaniel J. Smith | fc2f407 | 2018-01-21 06:44:07 -0800 | [diff] [blame] | 37 | .. this function name is too big to fit in the ascii-art table below |
| 38 | .. |coroutine-origin-link| replace:: :func:`sys.set_coroutine_origin_tracking_depth` |
| 39 | |
Xiang Zhang | a6902e6 | 2017-04-13 10:38:28 +0800 | [diff] [blame] | 40 | +-----------+-------------------+---------------------------+ |
| 41 | | Type | Attribute | Description | |
| 42 | +===========+===================+===========================+ |
| 43 | | module | __doc__ | documentation string | |
| 44 | +-----------+-------------------+---------------------------+ |
| 45 | | | __file__ | filename (missing for | |
| 46 | | | | built-in modules) | |
| 47 | +-----------+-------------------+---------------------------+ |
| 48 | | class | __doc__ | documentation string | |
| 49 | +-----------+-------------------+---------------------------+ |
| 50 | | | __name__ | name with which this | |
| 51 | | | | class was defined | |
| 52 | +-----------+-------------------+---------------------------+ |
| 53 | | | __qualname__ | qualified name | |
| 54 | +-----------+-------------------+---------------------------+ |
| 55 | | | __module__ | name of module in which | |
| 56 | | | | this class was defined | |
| 57 | +-----------+-------------------+---------------------------+ |
| 58 | | method | __doc__ | documentation string | |
| 59 | +-----------+-------------------+---------------------------+ |
| 60 | | | __name__ | name with which this | |
| 61 | | | | method was defined | |
| 62 | +-----------+-------------------+---------------------------+ |
| 63 | | | __qualname__ | qualified name | |
| 64 | +-----------+-------------------+---------------------------+ |
| 65 | | | __func__ | function object | |
| 66 | | | | containing implementation | |
| 67 | | | | of method | |
| 68 | +-----------+-------------------+---------------------------+ |
| 69 | | | __self__ | instance to which this | |
| 70 | | | | method is bound, or | |
| 71 | | | | ``None`` | |
| 72 | +-----------+-------------------+---------------------------+ |
| 73 | | function | __doc__ | documentation string | |
| 74 | +-----------+-------------------+---------------------------+ |
| 75 | | | __name__ | name with which this | |
| 76 | | | | function was defined | |
| 77 | +-----------+-------------------+---------------------------+ |
| 78 | | | __qualname__ | qualified name | |
| 79 | +-----------+-------------------+---------------------------+ |
| 80 | | | __code__ | code object containing | |
| 81 | | | | compiled function | |
| 82 | | | | :term:`bytecode` | |
| 83 | +-----------+-------------------+---------------------------+ |
| 84 | | | __defaults__ | tuple of any default | |
| 85 | | | | values for positional or | |
| 86 | | | | keyword parameters | |
| 87 | +-----------+-------------------+---------------------------+ |
| 88 | | | __kwdefaults__ | mapping of any default | |
| 89 | | | | values for keyword-only | |
| 90 | | | | parameters | |
| 91 | +-----------+-------------------+---------------------------+ |
| 92 | | | __globals__ | global namespace in which | |
| 93 | | | | this function was defined | |
| 94 | +-----------+-------------------+---------------------------+ |
| 95 | | | __annotations__ | mapping of parameters | |
| 96 | | | | names to annotations; | |
| 97 | | | | ``"return"`` key is | |
| 98 | | | | reserved for return | |
| 99 | | | | annotations. | |
| 100 | +-----------+-------------------+---------------------------+ |
| 101 | | traceback | tb_frame | frame object at this | |
| 102 | | | | level | |
| 103 | +-----------+-------------------+---------------------------+ |
| 104 | | | tb_lasti | index of last attempted | |
| 105 | | | | instruction in bytecode | |
| 106 | +-----------+-------------------+---------------------------+ |
| 107 | | | tb_lineno | current line number in | |
| 108 | | | | Python source code | |
| 109 | +-----------+-------------------+---------------------------+ |
| 110 | | | tb_next | next inner traceback | |
| 111 | | | | object (called by this | |
| 112 | | | | level) | |
| 113 | +-----------+-------------------+---------------------------+ |
| 114 | | frame | f_back | next outer frame object | |
| 115 | | | | (this frame's caller) | |
| 116 | +-----------+-------------------+---------------------------+ |
| 117 | | | f_builtins | builtins namespace seen | |
| 118 | | | | by this frame | |
| 119 | +-----------+-------------------+---------------------------+ |
| 120 | | | f_code | code object being | |
| 121 | | | | executed in this frame | |
| 122 | +-----------+-------------------+---------------------------+ |
| 123 | | | f_globals | global namespace seen by | |
| 124 | | | | this frame | |
| 125 | +-----------+-------------------+---------------------------+ |
| 126 | | | f_lasti | index of last attempted | |
| 127 | | | | instruction in bytecode | |
| 128 | +-----------+-------------------+---------------------------+ |
| 129 | | | f_lineno | current line number in | |
| 130 | | | | Python source code | |
| 131 | +-----------+-------------------+---------------------------+ |
| 132 | | | f_locals | local namespace seen by | |
| 133 | | | | this frame | |
| 134 | +-----------+-------------------+---------------------------+ |
Xiang Zhang | a6902e6 | 2017-04-13 10:38:28 +0800 | [diff] [blame] | 135 | | | f_trace | tracing function for this | |
| 136 | | | | frame, or ``None`` | |
| 137 | +-----------+-------------------+---------------------------+ |
| 138 | | code | co_argcount | number of arguments (not | |
| 139 | | | | including keyword only | |
| 140 | | | | arguments, \* or \*\* | |
| 141 | | | | args) | |
| 142 | +-----------+-------------------+---------------------------+ |
| 143 | | | co_code | string of raw compiled | |
| 144 | | | | bytecode | |
| 145 | +-----------+-------------------+---------------------------+ |
| 146 | | | co_cellvars | tuple of names of cell | |
| 147 | | | | variables (referenced by | |
| 148 | | | | containing scopes) | |
| 149 | +-----------+-------------------+---------------------------+ |
| 150 | | | co_consts | tuple of constants used | |
| 151 | | | | in the bytecode | |
| 152 | +-----------+-------------------+---------------------------+ |
| 153 | | | co_filename | name of file in which | |
| 154 | | | | this code object was | |
| 155 | | | | created | |
| 156 | +-----------+-------------------+---------------------------+ |
| 157 | | | co_firstlineno | number of first line in | |
| 158 | | | | Python source code | |
| 159 | +-----------+-------------------+---------------------------+ |
| 160 | | | co_flags | bitmap of ``CO_*`` flags, | |
| 161 | | | | read more :ref:`here | |
| 162 | | | | <inspect-module-co-flags>`| |
| 163 | +-----------+-------------------+---------------------------+ |
| 164 | | | co_lnotab | encoded mapping of line | |
| 165 | | | | numbers to bytecode | |
| 166 | | | | indices | |
| 167 | +-----------+-------------------+---------------------------+ |
| 168 | | | co_freevars | tuple of names of free | |
| 169 | | | | variables (referenced via | |
| 170 | | | | a function's closure) | |
| 171 | +-----------+-------------------+---------------------------+ |
| 172 | | | co_kwonlyargcount | number of keyword only | |
| 173 | | | | arguments (not including | |
| 174 | | | | \*\* arg) | |
| 175 | +-----------+-------------------+---------------------------+ |
| 176 | | | co_name | name with which this code | |
| 177 | | | | object was defined | |
| 178 | +-----------+-------------------+---------------------------+ |
| 179 | | | co_names | tuple of names of local | |
| 180 | | | | variables | |
| 181 | +-----------+-------------------+---------------------------+ |
| 182 | | | co_nlocals | number of local variables | |
| 183 | +-----------+-------------------+---------------------------+ |
| 184 | | | co_stacksize | virtual machine stack | |
| 185 | | | | space required | |
| 186 | +-----------+-------------------+---------------------------+ |
| 187 | | | co_varnames | tuple of names of | |
| 188 | | | | arguments and local | |
| 189 | | | | variables | |
| 190 | +-----------+-------------------+---------------------------+ |
| 191 | | generator | __name__ | name | |
| 192 | +-----------+-------------------+---------------------------+ |
| 193 | | | __qualname__ | qualified name | |
| 194 | +-----------+-------------------+---------------------------+ |
| 195 | | | gi_frame | frame | |
| 196 | +-----------+-------------------+---------------------------+ |
| 197 | | | gi_running | is the generator running? | |
| 198 | +-----------+-------------------+---------------------------+ |
| 199 | | | gi_code | code | |
| 200 | +-----------+-------------------+---------------------------+ |
| 201 | | | gi_yieldfrom | object being iterated by | |
| 202 | | | | ``yield from``, or | |
| 203 | | | | ``None`` | |
| 204 | +-----------+-------------------+---------------------------+ |
| 205 | | coroutine | __name__ | name | |
| 206 | +-----------+-------------------+---------------------------+ |
| 207 | | | __qualname__ | qualified name | |
| 208 | +-----------+-------------------+---------------------------+ |
| 209 | | | cr_await | object being awaited on, | |
| 210 | | | | or ``None`` | |
| 211 | +-----------+-------------------+---------------------------+ |
| 212 | | | cr_frame | frame | |
| 213 | +-----------+-------------------+---------------------------+ |
| 214 | | | cr_running | is the coroutine running? | |
| 215 | +-----------+-------------------+---------------------------+ |
| 216 | | | cr_code | code | |
| 217 | +-----------+-------------------+---------------------------+ |
Nathaniel J. Smith | fc2f407 | 2018-01-21 06:44:07 -0800 | [diff] [blame] | 218 | | | cr_origin | where coroutine was | |
| 219 | | | | created, or ``None``. See | |
| 220 | | | | |coroutine-origin-link| | |
| 221 | +-----------+-------------------+---------------------------+ |
Xiang Zhang | a6902e6 | 2017-04-13 10:38:28 +0800 | [diff] [blame] | 222 | | builtin | __doc__ | documentation string | |
| 223 | +-----------+-------------------+---------------------------+ |
| 224 | | | __name__ | original name of this | |
| 225 | | | | function or method | |
| 226 | +-----------+-------------------+---------------------------+ |
| 227 | | | __qualname__ | qualified name | |
| 228 | +-----------+-------------------+---------------------------+ |
| 229 | | | __self__ | instance to which a | |
| 230 | | | | method is bound, or | |
| 231 | | | | ``None`` | |
| 232 | +-----------+-------------------+---------------------------+ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 233 | |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 234 | .. versionchanged:: 3.5 |
| 235 | |
Yury Selivanov | 5fbad3c | 2015-08-17 13:04:41 -0400 | [diff] [blame] | 236 | Add ``__qualname__`` and ``gi_yieldfrom`` attributes to generators. |
| 237 | |
| 238 | The ``__name__`` attribute of generators is now set from the function |
| 239 | name, instead of the code name, and it can now be modified. |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 240 | |
Nathaniel J. Smith | fc2f407 | 2018-01-21 06:44:07 -0800 | [diff] [blame] | 241 | .. versionchanged:: 3.7 |
| 242 | |
| 243 | Add ``cr_origin`` attribute to coroutines. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 244 | |
| 245 | .. function:: getmembers(object[, predicate]) |
| 246 | |
Brian Curtin | df826f3 | 2018-04-26 19:48:26 -0400 | [diff] [blame] | 247 | Return all the members of an object in a list of ``(name, value)`` |
| 248 | pairs sorted by name. If the optional *predicate* argument—which will be |
| 249 | called with the ``value`` object of each member—is supplied, only members |
| 250 | for which the predicate returns a true value are included. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 251 | |
Christian Heimes | 7f04431 | 2008-01-06 17:05:40 +0000 | [diff] [blame] | 252 | .. note:: |
| 253 | |
Ethan Furman | 63c141c | 2013-10-18 00:27:39 -0700 | [diff] [blame] | 254 | :func:`getmembers` will only return class attributes defined in the |
| 255 | metaclass when the argument is a class and those attributes have been |
| 256 | listed in the metaclass' custom :meth:`__dir__`. |
Christian Heimes | 7f04431 | 2008-01-06 17:05:40 +0000 | [diff] [blame] | 257 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 258 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 259 | .. function:: getmodulename(path) |
| 260 | |
| 261 | Return the name of the module named by the file *path*, without including the |
Nick Coghlan | 76e0770 | 2012-07-18 23:14:57 +1000 | [diff] [blame] | 262 | names of enclosing packages. The file extension is checked against all of |
| 263 | the entries in :func:`importlib.machinery.all_suffixes`. If it matches, |
| 264 | the final path component is returned with the extension removed. |
| 265 | Otherwise, ``None`` is returned. |
| 266 | |
| 267 | Note that this function *only* returns a meaningful name for actual |
| 268 | Python modules - paths that potentially refer to Python packages will |
| 269 | still return ``None``. |
| 270 | |
| 271 | .. versionchanged:: 3.3 |
Yury Selivanov | 6dfbc5d | 2015-07-23 17:49:00 +0300 | [diff] [blame] | 272 | The function is based directly on :mod:`importlib`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 273 | |
| 274 | |
| 275 | .. function:: ismodule(object) |
| 276 | |
| 277 | Return true if the object is a module. |
| 278 | |
| 279 | |
| 280 | .. function:: isclass(object) |
| 281 | |
Georg Brandl | 39cadc3 | 2010-10-15 16:53:24 +0000 | [diff] [blame] | 282 | Return true if the object is a class, whether built-in or created in Python |
| 283 | code. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 284 | |
| 285 | |
| 286 | .. function:: ismethod(object) |
| 287 | |
Georg Brandl | 39cadc3 | 2010-10-15 16:53:24 +0000 | [diff] [blame] | 288 | Return true if the object is a bound method written in Python. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 289 | |
| 290 | |
| 291 | .. function:: isfunction(object) |
| 292 | |
Georg Brandl | 39cadc3 | 2010-10-15 16:53:24 +0000 | [diff] [blame] | 293 | Return true if the object is a Python function, which includes functions |
| 294 | created by a :term:`lambda` expression. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 295 | |
| 296 | |
Christian Heimes | 7131fd9 | 2008-02-19 14:21:46 +0000 | [diff] [blame] | 297 | .. function:: isgeneratorfunction(object) |
| 298 | |
| 299 | Return true if the object is a Python generator function. |
| 300 | |
Pablo Galindo | 7cd2543 | 2018-10-26 12:19:14 +0100 | [diff] [blame] | 301 | .. versionchanged:: 3.8 |
| 302 | Functions wrapped in :func:`functools.partial` now return true if the |
| 303 | wrapped function is a Python generator function. |
| 304 | |
Christian Heimes | 7131fd9 | 2008-02-19 14:21:46 +0000 | [diff] [blame] | 305 | |
| 306 | .. function:: isgenerator(object) |
| 307 | |
| 308 | Return true if the object is a generator. |
| 309 | |
| 310 | |
Yury Selivanov | f3e40fa | 2015-05-21 11:50:30 -0400 | [diff] [blame] | 311 | .. function:: iscoroutinefunction(object) |
| 312 | |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 313 | Return true if the object is a :term:`coroutine function` |
| 314 | (a function defined with an :keyword:`async def` syntax). |
Yury Selivanov | f3e40fa | 2015-05-21 11:50:30 -0400 | [diff] [blame] | 315 | |
Yury Selivanov | f3e40fa | 2015-05-21 11:50:30 -0400 | [diff] [blame] | 316 | .. versionadded:: 3.5 |
| 317 | |
Pablo Galindo | 7cd2543 | 2018-10-26 12:19:14 +0100 | [diff] [blame] | 318 | .. versionchanged:: 3.8 |
| 319 | Functions wrapped in :func:`functools.partial` now return true if the |
| 320 | wrapped function is a :term:`coroutine function`. |
| 321 | |
Yury Selivanov | f3e40fa | 2015-05-21 11:50:30 -0400 | [diff] [blame] | 322 | |
| 323 | .. function:: iscoroutine(object) |
| 324 | |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 325 | Return true if the object is a :term:`coroutine` created by an |
| 326 | :keyword:`async def` function. |
Yury Selivanov | f3e40fa | 2015-05-21 11:50:30 -0400 | [diff] [blame] | 327 | |
| 328 | .. versionadded:: 3.5 |
| 329 | |
| 330 | |
Yury Selivanov | fdbeb2b | 2015-07-03 13:11:35 -0400 | [diff] [blame] | 331 | .. function:: isawaitable(object) |
| 332 | |
| 333 | Return true if the object can be used in :keyword:`await` expression. |
| 334 | |
| 335 | Can also be used to distinguish generator-based coroutines from regular |
| 336 | generators:: |
| 337 | |
| 338 | def gen(): |
| 339 | yield |
| 340 | @types.coroutine |
| 341 | def gen_coro(): |
| 342 | yield |
| 343 | |
| 344 | assert not isawaitable(gen()) |
| 345 | assert isawaitable(gen_coro()) |
| 346 | |
| 347 | .. versionadded:: 3.5 |
| 348 | |
| 349 | |
Yury Selivanov | 0366004 | 2016-12-15 17:36:05 -0500 | [diff] [blame] | 350 | .. function:: isasyncgenfunction(object) |
| 351 | |
| 352 | Return true if the object is an :term:`asynchronous generator` function, |
| 353 | for example:: |
| 354 | |
| 355 | >>> async def agen(): |
| 356 | ... yield 1 |
| 357 | ... |
| 358 | >>> inspect.isasyncgenfunction(agen) |
| 359 | True |
| 360 | |
| 361 | .. versionadded:: 3.6 |
| 362 | |
Pablo Galindo | 7cd2543 | 2018-10-26 12:19:14 +0100 | [diff] [blame] | 363 | .. versionchanged:: 3.8 |
| 364 | Functions wrapped in :func:`functools.partial` now return true if the |
| 365 | wrapped function is a :term:`asynchronous generator` function. |
| 366 | |
Yury Selivanov | 0366004 | 2016-12-15 17:36:05 -0500 | [diff] [blame] | 367 | |
| 368 | .. function:: isasyncgen(object) |
| 369 | |
| 370 | Return true if the object is an :term:`asynchronous generator iterator` |
| 371 | created by an :term:`asynchronous generator` function. |
| 372 | |
| 373 | .. versionadded:: 3.6 |
| 374 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 375 | .. function:: istraceback(object) |
| 376 | |
| 377 | Return true if the object is a traceback. |
| 378 | |
| 379 | |
| 380 | .. function:: isframe(object) |
| 381 | |
| 382 | Return true if the object is a frame. |
| 383 | |
| 384 | |
| 385 | .. function:: iscode(object) |
| 386 | |
| 387 | Return true if the object is a code. |
| 388 | |
| 389 | |
| 390 | .. function:: isbuiltin(object) |
| 391 | |
Georg Brandl | 39cadc3 | 2010-10-15 16:53:24 +0000 | [diff] [blame] | 392 | Return true if the object is a built-in function or a bound built-in method. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 393 | |
| 394 | |
| 395 | .. function:: isroutine(object) |
| 396 | |
| 397 | Return true if the object is a user-defined or built-in function or method. |
| 398 | |
Georg Brandl | 39cadc3 | 2010-10-15 16:53:24 +0000 | [diff] [blame] | 399 | |
Christian Heimes | be5b30b | 2008-03-03 19:18:51 +0000 | [diff] [blame] | 400 | .. function:: isabstract(object) |
| 401 | |
| 402 | Return true if the object is an abstract base class. |
| 403 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 404 | |
| 405 | .. function:: ismethoddescriptor(object) |
| 406 | |
Georg Brandl | 39cadc3 | 2010-10-15 16:53:24 +0000 | [diff] [blame] | 407 | Return true if the object is a method descriptor, but not if |
| 408 | :func:`ismethod`, :func:`isclass`, :func:`isfunction` or :func:`isbuiltin` |
| 409 | are true. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 410 | |
Georg Brandl | e6bcc91 | 2008-05-12 18:05:20 +0000 | [diff] [blame] | 411 | This, for example, is true of ``int.__add__``. An object passing this test |
Martin Panter | bae5d81 | 2016-06-18 03:57:31 +0000 | [diff] [blame] | 412 | has a :meth:`~object.__get__` method but not a :meth:`~object.__set__` |
| 413 | method, but beyond that the set of attributes varies. A |
| 414 | :attr:`~definition.__name__` attribute is usually |
Georg Brandl | e6bcc91 | 2008-05-12 18:05:20 +0000 | [diff] [blame] | 415 | sensible, and :attr:`__doc__` often is. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 416 | |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 417 | Methods implemented via descriptors that also pass one of the other tests |
| 418 | return false from the :func:`ismethoddescriptor` test, simply because the |
| 419 | other tests promise more -- you can, e.g., count on having the |
Christian Heimes | ff73795 | 2007-11-27 10:40:20 +0000 | [diff] [blame] | 420 | :attr:`__func__` attribute (etc) when an object passes :func:`ismethod`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 421 | |
| 422 | |
| 423 | .. function:: isdatadescriptor(object) |
| 424 | |
| 425 | Return true if the object is a data descriptor. |
| 426 | |
Martin Panter | bae5d81 | 2016-06-18 03:57:31 +0000 | [diff] [blame] | 427 | Data descriptors have both a :attr:`~object.__get__` and a :attr:`~object.__set__` method. |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 428 | Examples are properties (defined in Python), getsets, and members. The |
| 429 | latter two are defined in C and there are more specific tests available for |
| 430 | those types, which is robust across Python implementations. Typically, data |
Martin Panter | bae5d81 | 2016-06-18 03:57:31 +0000 | [diff] [blame] | 431 | descriptors will also have :attr:`~definition.__name__` and :attr:`__doc__` attributes |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 432 | (properties, getsets, and members have both of these attributes), but this is |
| 433 | not guaranteed. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 434 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 435 | |
| 436 | .. function:: isgetsetdescriptor(object) |
| 437 | |
| 438 | Return true if the object is a getset descriptor. |
| 439 | |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 440 | .. impl-detail:: |
| 441 | |
| 442 | getsets are attributes defined in extension modules via |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 443 | :c:type:`PyGetSetDef` structures. For Python implementations without such |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 444 | types, this method will always return ``False``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 445 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 446 | |
| 447 | .. function:: ismemberdescriptor(object) |
| 448 | |
| 449 | Return true if the object is a member descriptor. |
| 450 | |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 451 | .. impl-detail:: |
| 452 | |
| 453 | Member descriptors are attributes defined in extension modules via |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 454 | :c:type:`PyMemberDef` structures. For Python implementations without such |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 455 | types, this method will always return ``False``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 456 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 457 | |
| 458 | .. _inspect-source: |
| 459 | |
| 460 | Retrieving source code |
| 461 | ---------------------- |
| 462 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 463 | .. function:: getdoc(object) |
| 464 | |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 465 | Get the documentation string for an object, cleaned up with :func:`cleandoc`. |
Serhiy Storchaka | 5cf2b72 | 2015-04-03 22:38:53 +0300 | [diff] [blame] | 466 | If the documentation string for an object is not provided and the object is |
| 467 | a class, a method, a property or a descriptor, retrieve the documentation |
| 468 | string from the inheritance hierarchy. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 469 | |
Berker Peksag | 4333d8b | 2015-07-30 18:06:09 +0300 | [diff] [blame] | 470 | .. versionchanged:: 3.5 |
| 471 | Documentation strings are now inherited if not overridden. |
| 472 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 473 | |
| 474 | .. function:: getcomments(object) |
| 475 | |
| 476 | Return in a single string any lines of comments immediately preceding the |
| 477 | object's source code (for a class, function, or method), or at the top of the |
Marco Buttu | 3f2155f | 2017-03-17 09:50:23 +0100 | [diff] [blame] | 478 | Python source file (if the object is a module). If the object's source code |
| 479 | is unavailable, return ``None``. This could happen if the object has been |
| 480 | defined in C or the interactive shell. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 481 | |
| 482 | |
| 483 | .. function:: getfile(object) |
| 484 | |
| 485 | Return the name of the (text or binary) file in which an object was defined. |
| 486 | This will fail with a :exc:`TypeError` if the object is a built-in module, |
| 487 | class, or function. |
| 488 | |
| 489 | |
| 490 | .. function:: getmodule(object) |
| 491 | |
| 492 | Try to guess which module an object was defined in. |
| 493 | |
| 494 | |
| 495 | .. function:: getsourcefile(object) |
| 496 | |
| 497 | Return the name of the Python source file in which an object was defined. This |
| 498 | will fail with a :exc:`TypeError` if the object is a built-in module, class, or |
| 499 | function. |
| 500 | |
| 501 | |
| 502 | .. function:: getsourcelines(object) |
| 503 | |
| 504 | Return a list of source lines and starting line number for an object. The |
| 505 | argument may be a module, class, method, function, traceback, frame, or code |
| 506 | object. The source code is returned as a list of the lines corresponding to the |
| 507 | object and the line number indicates where in the original source file the first |
Antoine Pitrou | 62ab10a0 | 2011-10-12 20:10:51 +0200 | [diff] [blame] | 508 | line of code was found. An :exc:`OSError` is raised if the source code cannot |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 509 | be retrieved. |
| 510 | |
Antoine Pitrou | 62ab10a0 | 2011-10-12 20:10:51 +0200 | [diff] [blame] | 511 | .. versionchanged:: 3.3 |
| 512 | :exc:`OSError` is raised instead of :exc:`IOError`, now an alias of the |
| 513 | former. |
| 514 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 515 | |
| 516 | .. function:: getsource(object) |
| 517 | |
| 518 | Return the text of the source code for an object. The argument may be a module, |
| 519 | class, method, function, traceback, frame, or code object. The source code is |
Antoine Pitrou | 62ab10a0 | 2011-10-12 20:10:51 +0200 | [diff] [blame] | 520 | returned as a single string. An :exc:`OSError` is raised if the source code |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 521 | cannot be retrieved. |
| 522 | |
Antoine Pitrou | 62ab10a0 | 2011-10-12 20:10:51 +0200 | [diff] [blame] | 523 | .. versionchanged:: 3.3 |
| 524 | :exc:`OSError` is raised instead of :exc:`IOError`, now an alias of the |
| 525 | former. |
| 526 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 527 | |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 528 | .. function:: cleandoc(doc) |
| 529 | |
| 530 | Clean up indentation from docstrings that are indented to line up with blocks |
Senthil Kumaran | ebd84e3 | 2016-05-29 20:36:58 -0700 | [diff] [blame] | 531 | of code. |
| 532 | |
| 533 | All leading whitespace is removed from the first line. Any leading whitespace |
| 534 | that can be uniformly removed from the second line onwards is removed. Empty |
| 535 | lines at the beginning and end are subsequently removed. Also, all tabs are |
| 536 | expanded to spaces. |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 537 | |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 538 | |
Andrew Svetlov | 4e48bf9 | 2012-08-13 17:10:28 +0300 | [diff] [blame] | 539 | .. _inspect-signature-object: |
| 540 | |
Georg Brandl | e471772 | 2012-08-14 09:45:28 +0200 | [diff] [blame] | 541 | Introspecting callables with the Signature object |
| 542 | ------------------------------------------------- |
Andrew Svetlov | 4e48bf9 | 2012-08-13 17:10:28 +0300 | [diff] [blame] | 543 | |
| 544 | .. versionadded:: 3.3 |
| 545 | |
Georg Brandl | e471772 | 2012-08-14 09:45:28 +0200 | [diff] [blame] | 546 | The Signature object represents the call signature of a callable object and its |
| 547 | return annotation. To retrieve a Signature object, use the :func:`signature` |
| 548 | function. |
Andrew Svetlov | 4e48bf9 | 2012-08-13 17:10:28 +0300 | [diff] [blame] | 549 | |
Yury Selivanov | bcd4fc1 | 2015-05-20 14:30:08 -0400 | [diff] [blame] | 550 | .. function:: signature(callable, \*, follow_wrapped=True) |
Andrew Svetlov | 4e48bf9 | 2012-08-13 17:10:28 +0300 | [diff] [blame] | 551 | |
Georg Brandl | e471772 | 2012-08-14 09:45:28 +0200 | [diff] [blame] | 552 | Return a :class:`Signature` object for the given ``callable``:: |
Andrew Svetlov | 4e48bf9 | 2012-08-13 17:10:28 +0300 | [diff] [blame] | 553 | |
| 554 | >>> from inspect import signature |
| 555 | >>> def foo(a, *, b:int, **kwargs): |
| 556 | ... pass |
| 557 | |
| 558 | >>> sig = signature(foo) |
| 559 | |
| 560 | >>> str(sig) |
| 561 | '(a, *, b:int, **kwargs)' |
| 562 | |
| 563 | >>> str(sig.parameters['b']) |
| 564 | 'b:int' |
| 565 | |
| 566 | >>> sig.parameters['b'].annotation |
| 567 | <class 'int'> |
| 568 | |
Andrés Delfino | 271818f | 2018-09-14 14:13:09 -0300 | [diff] [blame] | 569 | Accepts a wide range of Python callables, from plain functions and classes to |
Georg Brandl | e471772 | 2012-08-14 09:45:28 +0200 | [diff] [blame] | 570 | :func:`functools.partial` objects. |
Andrew Svetlov | 4e48bf9 | 2012-08-13 17:10:28 +0300 | [diff] [blame] | 571 | |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 572 | Raises :exc:`ValueError` if no signature can be provided, and |
| 573 | :exc:`TypeError` if that type of object is not supported. |
| 574 | |
Lysandros Nikolaou | 1aeeaeb | 2019-03-10 12:30:11 +0100 | [diff] [blame] | 575 | A slash(/) in the signature of a function denotes that the parameters prior |
| 576 | to it are positional-only. For more info, see |
| 577 | :ref:`the FAQ entry on positional-only parameters <faq-positional-only-arguments>`. |
| 578 | |
Yury Selivanov | bcd4fc1 | 2015-05-20 14:30:08 -0400 | [diff] [blame] | 579 | .. versionadded:: 3.5 |
| 580 | ``follow_wrapped`` parameter. Pass ``False`` to get a signature of |
| 581 | ``callable`` specifically (``callable.__wrapped__`` will not be used to |
| 582 | unwrap decorated callables.) |
| 583 | |
Andrew Svetlov | 4e48bf9 | 2012-08-13 17:10:28 +0300 | [diff] [blame] | 584 | .. note:: |
| 585 | |
Georg Brandl | e471772 | 2012-08-14 09:45:28 +0200 | [diff] [blame] | 586 | Some callables may not be introspectable in certain implementations of |
Yury Selivanov | d71e52f | 2014-01-30 00:22:57 -0500 | [diff] [blame] | 587 | Python. For example, in CPython, some built-in functions defined in |
| 588 | C provide no metadata about their arguments. |
Andrew Svetlov | 4e48bf9 | 2012-08-13 17:10:28 +0300 | [diff] [blame] | 589 | |
| 590 | |
Yury Selivanov | 7835689 | 2014-01-30 00:10:54 -0500 | [diff] [blame] | 591 | .. class:: Signature(parameters=None, \*, return_annotation=Signature.empty) |
Andrew Svetlov | 4e48bf9 | 2012-08-13 17:10:28 +0300 | [diff] [blame] | 592 | |
Georg Brandl | e471772 | 2012-08-14 09:45:28 +0200 | [diff] [blame] | 593 | A Signature object represents the call signature of a function and its return |
| 594 | annotation. For each parameter accepted by the function it stores a |
| 595 | :class:`Parameter` object in its :attr:`parameters` collection. |
Andrew Svetlov | 4e48bf9 | 2012-08-13 17:10:28 +0300 | [diff] [blame] | 596 | |
Yury Selivanov | 7835689 | 2014-01-30 00:10:54 -0500 | [diff] [blame] | 597 | The optional *parameters* argument is a sequence of :class:`Parameter` |
| 598 | objects, which is validated to check that there are no parameters with |
| 599 | duplicate names, and that the parameters are in the right order, i.e. |
| 600 | positional-only first, then positional-or-keyword, and that parameters with |
| 601 | defaults follow parameters without defaults. |
| 602 | |
| 603 | The optional *return_annotation* argument, can be an arbitrary Python object, |
| 604 | is the "return" annotation of the callable. |
| 605 | |
Georg Brandl | e471772 | 2012-08-14 09:45:28 +0200 | [diff] [blame] | 606 | Signature objects are *immutable*. Use :meth:`Signature.replace` to make a |
| 607 | modified copy. |
Andrew Svetlov | 4e48bf9 | 2012-08-13 17:10:28 +0300 | [diff] [blame] | 608 | |
Yury Selivanov | 67d727e | 2014-03-29 13:24:14 -0400 | [diff] [blame] | 609 | .. versionchanged:: 3.5 |
Yury Selivanov | 67ae50e | 2014-04-08 11:46:50 -0400 | [diff] [blame] | 610 | Signature objects are picklable and hashable. |
Yury Selivanov | 67d727e | 2014-03-29 13:24:14 -0400 | [diff] [blame] | 611 | |
Andrew Svetlov | 4e48bf9 | 2012-08-13 17:10:28 +0300 | [diff] [blame] | 612 | .. attribute:: Signature.empty |
| 613 | |
| 614 | A special class-level marker to specify absence of a return annotation. |
| 615 | |
| 616 | .. attribute:: Signature.parameters |
| 617 | |
| 618 | An ordered mapping of parameters' names to the corresponding |
larryhastings | f36ba12 | 2018-01-28 11:13:09 -0800 | [diff] [blame] | 619 | :class:`Parameter` objects. Parameters appear in strict definition |
| 620 | order, including keyword-only parameters. |
| 621 | |
| 622 | .. versionchanged:: 3.7 |
| 623 | Python only explicitly guaranteed that it preserved the declaration |
| 624 | order of keyword-only parameters as of version 3.7, although in practice |
| 625 | this order had always been preserved in Python 3. |
Andrew Svetlov | 4e48bf9 | 2012-08-13 17:10:28 +0300 | [diff] [blame] | 626 | |
| 627 | .. attribute:: Signature.return_annotation |
| 628 | |
Georg Brandl | e471772 | 2012-08-14 09:45:28 +0200 | [diff] [blame] | 629 | The "return" annotation for the callable. If the callable has no "return" |
| 630 | annotation, this attribute is set to :attr:`Signature.empty`. |
Andrew Svetlov | 4e48bf9 | 2012-08-13 17:10:28 +0300 | [diff] [blame] | 631 | |
| 632 | .. method:: Signature.bind(*args, **kwargs) |
| 633 | |
Georg Brandl | e471772 | 2012-08-14 09:45:28 +0200 | [diff] [blame] | 634 | Create a mapping from positional and keyword arguments to parameters. |
| 635 | Returns :class:`BoundArguments` if ``*args`` and ``**kwargs`` match the |
| 636 | signature, or raises a :exc:`TypeError`. |
Andrew Svetlov | 4e48bf9 | 2012-08-13 17:10:28 +0300 | [diff] [blame] | 637 | |
| 638 | .. method:: Signature.bind_partial(*args, **kwargs) |
| 639 | |
Georg Brandl | e471772 | 2012-08-14 09:45:28 +0200 | [diff] [blame] | 640 | Works the same way as :meth:`Signature.bind`, but allows the omission of |
| 641 | some required arguments (mimics :func:`functools.partial` behavior.) |
| 642 | Returns :class:`BoundArguments`, or raises a :exc:`TypeError` if the |
| 643 | passed arguments do not match the signature. |
Andrew Svetlov | 4e48bf9 | 2012-08-13 17:10:28 +0300 | [diff] [blame] | 644 | |
Ezio Melotti | 8429b67 | 2012-09-14 06:35:09 +0300 | [diff] [blame] | 645 | .. method:: Signature.replace(*[, parameters][, return_annotation]) |
Andrew Svetlov | 4e48bf9 | 2012-08-13 17:10:28 +0300 | [diff] [blame] | 646 | |
Georg Brandl | e471772 | 2012-08-14 09:45:28 +0200 | [diff] [blame] | 647 | Create a new Signature instance based on the instance replace was invoked |
| 648 | on. It is possible to pass different ``parameters`` and/or |
| 649 | ``return_annotation`` to override the corresponding properties of the base |
| 650 | signature. To remove return_annotation from the copied Signature, pass in |
| 651 | :attr:`Signature.empty`. |
Andrew Svetlov | 4e48bf9 | 2012-08-13 17:10:28 +0300 | [diff] [blame] | 652 | |
| 653 | :: |
| 654 | |
| 655 | >>> def test(a, b): |
| 656 | ... pass |
| 657 | >>> sig = signature(test) |
| 658 | >>> new_sig = sig.replace(return_annotation="new return anno") |
| 659 | >>> str(new_sig) |
| 660 | "(a, b) -> 'new return anno'" |
| 661 | |
Yury Selivanov | bcd4fc1 | 2015-05-20 14:30:08 -0400 | [diff] [blame] | 662 | .. classmethod:: Signature.from_callable(obj, \*, follow_wrapped=True) |
Yury Selivanov | da39645 | 2014-03-27 12:09:24 -0400 | [diff] [blame] | 663 | |
| 664 | Return a :class:`Signature` (or its subclass) object for a given callable |
Yury Selivanov | bcd4fc1 | 2015-05-20 14:30:08 -0400 | [diff] [blame] | 665 | ``obj``. Pass ``follow_wrapped=False`` to get a signature of ``obj`` |
| 666 | without unwrapping its ``__wrapped__`` chain. |
Yury Selivanov | da39645 | 2014-03-27 12:09:24 -0400 | [diff] [blame] | 667 | |
Yury Selivanov | bcd4fc1 | 2015-05-20 14:30:08 -0400 | [diff] [blame] | 668 | This method simplifies subclassing of :class:`Signature`:: |
Yury Selivanov | da39645 | 2014-03-27 12:09:24 -0400 | [diff] [blame] | 669 | |
| 670 | class MySignature(Signature): |
| 671 | pass |
| 672 | sig = MySignature.from_callable(min) |
| 673 | assert isinstance(sig, MySignature) |
| 674 | |
Yury Selivanov | 232b934 | 2014-03-29 13:18:30 -0400 | [diff] [blame] | 675 | .. versionadded:: 3.5 |
| 676 | |
Andrew Svetlov | 4e48bf9 | 2012-08-13 17:10:28 +0300 | [diff] [blame] | 677 | |
Yury Selivanov | 7835689 | 2014-01-30 00:10:54 -0500 | [diff] [blame] | 678 | .. class:: Parameter(name, kind, \*, default=Parameter.empty, annotation=Parameter.empty) |
Andrew Svetlov | 4e48bf9 | 2012-08-13 17:10:28 +0300 | [diff] [blame] | 679 | |
Georg Brandl | e471772 | 2012-08-14 09:45:28 +0200 | [diff] [blame] | 680 | Parameter objects are *immutable*. Instead of modifying a Parameter object, |
Andrew Svetlov | 4e48bf9 | 2012-08-13 17:10:28 +0300 | [diff] [blame] | 681 | you can use :meth:`Parameter.replace` to create a modified copy. |
| 682 | |
Yury Selivanov | 67d727e | 2014-03-29 13:24:14 -0400 | [diff] [blame] | 683 | .. versionchanged:: 3.5 |
Yury Selivanov | 67ae50e | 2014-04-08 11:46:50 -0400 | [diff] [blame] | 684 | Parameter objects are picklable and hashable. |
Yury Selivanov | 67d727e | 2014-03-29 13:24:14 -0400 | [diff] [blame] | 685 | |
Andrew Svetlov | 4e48bf9 | 2012-08-13 17:10:28 +0300 | [diff] [blame] | 686 | .. attribute:: Parameter.empty |
| 687 | |
Georg Brandl | e471772 | 2012-08-14 09:45:28 +0200 | [diff] [blame] | 688 | A special class-level marker to specify absence of default values and |
| 689 | annotations. |
Andrew Svetlov | 4e48bf9 | 2012-08-13 17:10:28 +0300 | [diff] [blame] | 690 | |
| 691 | .. attribute:: Parameter.name |
| 692 | |
Yury Selivanov | 2393dca | 2014-01-27 15:07:58 -0500 | [diff] [blame] | 693 | The name of the parameter as a string. The name must be a valid |
| 694 | Python identifier. |
Andrew Svetlov | 4e48bf9 | 2012-08-13 17:10:28 +0300 | [diff] [blame] | 695 | |
Nick Coghlan | b4b966e | 2016-06-04 14:40:03 -0700 | [diff] [blame] | 696 | .. impl-detail:: |
| 697 | |
| 698 | CPython generates implicit parameter names of the form ``.0`` on the |
| 699 | code objects used to implement comprehensions and generator |
| 700 | expressions. |
| 701 | |
| 702 | .. versionchanged:: 3.6 |
| 703 | These parameter names are exposed by this module as names like |
| 704 | ``implicit0``. |
| 705 | |
Andrew Svetlov | 4e48bf9 | 2012-08-13 17:10:28 +0300 | [diff] [blame] | 706 | .. attribute:: Parameter.default |
| 707 | |
Georg Brandl | e471772 | 2012-08-14 09:45:28 +0200 | [diff] [blame] | 708 | The default value for the parameter. If the parameter has no default |
Andrew Svetlov | 4e48bf9 | 2012-08-13 17:10:28 +0300 | [diff] [blame] | 709 | value, this attribute is set to :attr:`Parameter.empty`. |
| 710 | |
| 711 | .. attribute:: Parameter.annotation |
| 712 | |
Georg Brandl | e471772 | 2012-08-14 09:45:28 +0200 | [diff] [blame] | 713 | The annotation for the parameter. If the parameter has no annotation, |
Andrew Svetlov | 4e48bf9 | 2012-08-13 17:10:28 +0300 | [diff] [blame] | 714 | this attribute is set to :attr:`Parameter.empty`. |
| 715 | |
| 716 | .. attribute:: Parameter.kind |
| 717 | |
Georg Brandl | e471772 | 2012-08-14 09:45:28 +0200 | [diff] [blame] | 718 | Describes how argument values are bound to the parameter. Possible values |
| 719 | (accessible via :class:`Parameter`, like ``Parameter.KEYWORD_ONLY``): |
Andrew Svetlov | 4e48bf9 | 2012-08-13 17:10:28 +0300 | [diff] [blame] | 720 | |
Georg Brandl | 44ea77b | 2013-03-28 13:28:44 +0100 | [diff] [blame] | 721 | .. tabularcolumns:: |l|L| |
| 722 | |
Andrew Svetlov | 4e48bf9 | 2012-08-13 17:10:28 +0300 | [diff] [blame] | 723 | +------------------------+----------------------------------------------+ |
| 724 | | Name | Meaning | |
| 725 | +========================+==============================================+ |
| 726 | | *POSITIONAL_ONLY* | Value must be supplied as a positional | |
| 727 | | | argument. | |
| 728 | | | | |
| 729 | | | Python has no explicit syntax for defining | |
| 730 | | | positional-only parameters, but many built-in| |
| 731 | | | and extension module functions (especially | |
| 732 | | | those that accept only one or two parameters)| |
| 733 | | | accept them. | |
| 734 | +------------------------+----------------------------------------------+ |
| 735 | | *POSITIONAL_OR_KEYWORD*| Value may be supplied as either a keyword or | |
| 736 | | | positional argument (this is the standard | |
| 737 | | | binding behaviour for functions implemented | |
| 738 | | | in Python.) | |
| 739 | +------------------------+----------------------------------------------+ |
| 740 | | *VAR_POSITIONAL* | A tuple of positional arguments that aren't | |
| 741 | | | bound to any other parameter. This | |
| 742 | | | corresponds to a ``*args`` parameter in a | |
| 743 | | | Python function definition. | |
| 744 | +------------------------+----------------------------------------------+ |
| 745 | | *KEYWORD_ONLY* | Value must be supplied as a keyword argument.| |
| 746 | | | Keyword only parameters are those which | |
| 747 | | | appear after a ``*`` or ``*args`` entry in a | |
| 748 | | | Python function definition. | |
| 749 | +------------------------+----------------------------------------------+ |
| 750 | | *VAR_KEYWORD* | A dict of keyword arguments that aren't bound| |
| 751 | | | to any other parameter. This corresponds to a| |
| 752 | | | ``**kwargs`` parameter in a Python function | |
| 753 | | | definition. | |
| 754 | +------------------------+----------------------------------------------+ |
| 755 | |
Andrew Svetlov | eed1808 | 2012-08-13 18:23:54 +0300 | [diff] [blame] | 756 | Example: print all keyword-only arguments without default values:: |
Andrew Svetlov | 4e48bf9 | 2012-08-13 17:10:28 +0300 | [diff] [blame] | 757 | |
| 758 | >>> def foo(a, b, *, c, d=10): |
| 759 | ... pass |
| 760 | |
| 761 | >>> sig = signature(foo) |
| 762 | >>> for param in sig.parameters.values(): |
| 763 | ... if (param.kind == param.KEYWORD_ONLY and |
| 764 | ... param.default is param.empty): |
| 765 | ... print('Parameter:', param) |
| 766 | Parameter: c |
| 767 | |
Dong-hee Na | 4aa3006 | 2018-06-08 12:46:31 +0900 | [diff] [blame] | 768 | .. attribute:: Parameter.kind.description |
| 769 | |
| 770 | Describes a enum value of Parameter.kind. |
| 771 | |
Dong-hee Na | 4f54867 | 2018-06-09 01:07:52 +0900 | [diff] [blame] | 772 | .. versionadded:: 3.8 |
| 773 | |
Dong-hee Na | 4aa3006 | 2018-06-08 12:46:31 +0900 | [diff] [blame] | 774 | Example: print all descriptions of arguments:: |
| 775 | |
| 776 | >>> def foo(a, b, *, c, d=10): |
| 777 | ... pass |
| 778 | |
| 779 | >>> sig = signature(foo) |
| 780 | >>> for param in sig.parameters.values(): |
| 781 | ... print(param.kind.description) |
| 782 | positional or keyword |
| 783 | positional or keyword |
| 784 | keyword-only |
| 785 | keyword-only |
| 786 | |
Ezio Melotti | 8429b67 | 2012-09-14 06:35:09 +0300 | [diff] [blame] | 787 | .. method:: Parameter.replace(*[, name][, kind][, default][, annotation]) |
Andrew Svetlov | 4e48bf9 | 2012-08-13 17:10:28 +0300 | [diff] [blame] | 788 | |
Georg Brandl | e471772 | 2012-08-14 09:45:28 +0200 | [diff] [blame] | 789 | Create a new Parameter instance based on the instance replaced was invoked |
| 790 | on. To override a :class:`Parameter` attribute, pass the corresponding |
| 791 | argument. To remove a default value or/and an annotation from a |
| 792 | Parameter, pass :attr:`Parameter.empty`. |
Andrew Svetlov | 4e48bf9 | 2012-08-13 17:10:28 +0300 | [diff] [blame] | 793 | |
| 794 | :: |
| 795 | |
| 796 | >>> from inspect import Parameter |
| 797 | >>> param = Parameter('foo', Parameter.KEYWORD_ONLY, default=42) |
| 798 | >>> str(param) |
| 799 | 'foo=42' |
| 800 | |
| 801 | >>> str(param.replace()) # Will create a shallow copy of 'param' |
| 802 | 'foo=42' |
| 803 | |
| 804 | >>> str(param.replace(default=Parameter.empty, annotation='spam')) |
| 805 | "foo:'spam'" |
| 806 | |
Yury Selivanov | 2393dca | 2014-01-27 15:07:58 -0500 | [diff] [blame] | 807 | .. versionchanged:: 3.4 |
| 808 | In Python 3.3 Parameter objects were allowed to have ``name`` set |
| 809 | to ``None`` if their ``kind`` was set to ``POSITIONAL_ONLY``. |
| 810 | This is no longer permitted. |
Andrew Svetlov | 4e48bf9 | 2012-08-13 17:10:28 +0300 | [diff] [blame] | 811 | |
| 812 | .. class:: BoundArguments |
| 813 | |
| 814 | Result of a :meth:`Signature.bind` or :meth:`Signature.bind_partial` call. |
| 815 | Holds the mapping of arguments to the function's parameters. |
| 816 | |
| 817 | .. attribute:: BoundArguments.arguments |
| 818 | |
| 819 | An ordered, mutable mapping (:class:`collections.OrderedDict`) of |
Georg Brandl | e471772 | 2012-08-14 09:45:28 +0200 | [diff] [blame] | 820 | parameters' names to arguments' values. Contains only explicitly bound |
| 821 | arguments. Changes in :attr:`arguments` will reflect in :attr:`args` and |
| 822 | :attr:`kwargs`. |
Andrew Svetlov | 4e48bf9 | 2012-08-13 17:10:28 +0300 | [diff] [blame] | 823 | |
Georg Brandl | e471772 | 2012-08-14 09:45:28 +0200 | [diff] [blame] | 824 | Should be used in conjunction with :attr:`Signature.parameters` for any |
| 825 | argument processing purposes. |
Andrew Svetlov | 4e48bf9 | 2012-08-13 17:10:28 +0300 | [diff] [blame] | 826 | |
| 827 | .. note:: |
| 828 | |
| 829 | Arguments for which :meth:`Signature.bind` or |
| 830 | :meth:`Signature.bind_partial` relied on a default value are skipped. |
Yury Selivanov | b907a51 | 2015-05-16 13:45:09 -0400 | [diff] [blame] | 831 | However, if needed, use :meth:`BoundArguments.apply_defaults` to add |
| 832 | them. |
Andrew Svetlov | 4e48bf9 | 2012-08-13 17:10:28 +0300 | [diff] [blame] | 833 | |
| 834 | .. attribute:: BoundArguments.args |
| 835 | |
Georg Brandl | e471772 | 2012-08-14 09:45:28 +0200 | [diff] [blame] | 836 | A tuple of positional arguments values. Dynamically computed from the |
| 837 | :attr:`arguments` attribute. |
Andrew Svetlov | 4e48bf9 | 2012-08-13 17:10:28 +0300 | [diff] [blame] | 838 | |
| 839 | .. attribute:: BoundArguments.kwargs |
| 840 | |
Georg Brandl | e471772 | 2012-08-14 09:45:28 +0200 | [diff] [blame] | 841 | A dict of keyword arguments values. Dynamically computed from the |
| 842 | :attr:`arguments` attribute. |
Andrew Svetlov | 4e48bf9 | 2012-08-13 17:10:28 +0300 | [diff] [blame] | 843 | |
Yury Selivanov | 8279619 | 2015-05-14 14:14:02 -0400 | [diff] [blame] | 844 | .. attribute:: BoundArguments.signature |
| 845 | |
| 846 | A reference to the parent :class:`Signature` object. |
| 847 | |
Yury Selivanov | b907a51 | 2015-05-16 13:45:09 -0400 | [diff] [blame] | 848 | .. method:: BoundArguments.apply_defaults() |
| 849 | |
| 850 | Set default values for missing arguments. |
| 851 | |
| 852 | For variable-positional arguments (``*args``) the default is an |
| 853 | empty tuple. |
| 854 | |
| 855 | For variable-keyword arguments (``**kwargs``) the default is an |
| 856 | empty dict. |
| 857 | |
| 858 | :: |
| 859 | |
| 860 | >>> def foo(a, b='ham', *args): pass |
| 861 | >>> ba = inspect.signature(foo).bind('spam') |
| 862 | >>> ba.apply_defaults() |
| 863 | >>> ba.arguments |
| 864 | OrderedDict([('a', 'spam'), ('b', 'ham'), ('args', ())]) |
| 865 | |
Berker Peksag | 5b3df5b | 2015-05-16 23:29:31 +0300 | [diff] [blame] | 866 | .. versionadded:: 3.5 |
| 867 | |
Georg Brandl | e471772 | 2012-08-14 09:45:28 +0200 | [diff] [blame] | 868 | The :attr:`args` and :attr:`kwargs` properties can be used to invoke |
| 869 | functions:: |
Andrew Svetlov | 4e48bf9 | 2012-08-13 17:10:28 +0300 | [diff] [blame] | 870 | |
| 871 | def test(a, *, b): |
Serhiy Storchaka | dba9039 | 2016-05-10 12:01:23 +0300 | [diff] [blame] | 872 | ... |
Andrew Svetlov | 4e48bf9 | 2012-08-13 17:10:28 +0300 | [diff] [blame] | 873 | |
| 874 | sig = signature(test) |
| 875 | ba = sig.bind(10, b=20) |
| 876 | test(*ba.args, **ba.kwargs) |
| 877 | |
| 878 | |
Georg Brandl | e471772 | 2012-08-14 09:45:28 +0200 | [diff] [blame] | 879 | .. seealso:: |
| 880 | |
| 881 | :pep:`362` - Function Signature Object. |
| 882 | The detailed specification, implementation details and examples. |
| 883 | |
| 884 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 885 | .. _inspect-classes-functions: |
| 886 | |
| 887 | Classes and functions |
| 888 | --------------------- |
| 889 | |
Georg Brandl | 3dd3388 | 2009-06-01 17:35:27 +0000 | [diff] [blame] | 890 | .. function:: getclasstree(classes, unique=False) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 891 | |
| 892 | Arrange the given list of classes into a hierarchy of nested lists. Where a |
| 893 | nested list appears, it contains classes derived from the class whose entry |
| 894 | immediately precedes the list. Each entry is a 2-tuple containing a class and a |
| 895 | tuple of its base classes. If the *unique* argument is true, exactly one entry |
| 896 | appears in the returned structure for each class in the given list. Otherwise, |
| 897 | classes using multiple inheritance and their descendants will appear multiple |
| 898 | times. |
| 899 | |
Yury Selivanov | 37dc2b2 | 2016-01-11 15:15:01 -0500 | [diff] [blame] | 900 | |
| 901 | .. function:: getargspec(func) |
| 902 | |
Nick Coghlan | 3c35fdb | 2016-12-02 20:29:57 +1000 | [diff] [blame] | 903 | Get the names and default values of a Python function's parameters. A |
Yury Selivanov | 37dc2b2 | 2016-01-11 15:15:01 -0500 | [diff] [blame] | 904 | :term:`named tuple` ``ArgSpec(args, varargs, keywords, defaults)`` is |
Nick Coghlan | 3c35fdb | 2016-12-02 20:29:57 +1000 | [diff] [blame] | 905 | returned. *args* is a list of the parameter names. *varargs* and *keywords* |
| 906 | are the names of the ``*`` and ``**`` parameters or ``None``. *defaults* is a |
Yury Selivanov | 37dc2b2 | 2016-01-11 15:15:01 -0500 | [diff] [blame] | 907 | tuple of default argument values or ``None`` if there are no default |
| 908 | arguments; if this tuple has *n* elements, they correspond to the last |
| 909 | *n* elements listed in *args*. |
| 910 | |
| 911 | .. deprecated:: 3.0 |
Nick Coghlan | 3c35fdb | 2016-12-02 20:29:57 +1000 | [diff] [blame] | 912 | Use :func:`getfullargspec` for an updated API that is usually a drop-in |
| 913 | replacement, but also correctly handles function annotations and |
| 914 | keyword-only parameters. |
| 915 | |
| 916 | Alternatively, use :func:`signature` and |
Yury Selivanov | 37dc2b2 | 2016-01-11 15:15:01 -0500 | [diff] [blame] | 917 | :ref:`Signature Object <inspect-signature-object>`, which provide a |
Nick Coghlan | 3c35fdb | 2016-12-02 20:29:57 +1000 | [diff] [blame] | 918 | more structured introspection API for callables. |
Yury Selivanov | 37dc2b2 | 2016-01-11 15:15:01 -0500 | [diff] [blame] | 919 | |
| 920 | |
Georg Brandl | 138bcb5 | 2007-09-12 19:04:21 +0000 | [diff] [blame] | 921 | .. function:: getfullargspec(func) |
| 922 | |
Nick Coghlan | 3c35fdb | 2016-12-02 20:29:57 +1000 | [diff] [blame] | 923 | Get the names and default values of a Python function's parameters. A |
Georg Brandl | 8240275 | 2010-01-09 09:48:46 +0000 | [diff] [blame] | 924 | :term:`named tuple` is returned: |
Georg Brandl | 138bcb5 | 2007-09-12 19:04:21 +0000 | [diff] [blame] | 925 | |
Georg Brandl | 3dd3388 | 2009-06-01 17:35:27 +0000 | [diff] [blame] | 926 | ``FullArgSpec(args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, |
| 927 | annotations)`` |
Georg Brandl | 138bcb5 | 2007-09-12 19:04:21 +0000 | [diff] [blame] | 928 | |
Nick Coghlan | 3c35fdb | 2016-12-02 20:29:57 +1000 | [diff] [blame] | 929 | *args* is a list of the positional parameter names. |
| 930 | *varargs* is the name of the ``*`` parameter or ``None`` if arbitrary |
| 931 | positional arguments are not accepted. |
| 932 | *varkw* is the name of the ``**`` parameter or ``None`` if arbitrary |
| 933 | keyword arguments are not accepted. |
| 934 | *defaults* is an *n*-tuple of default argument values corresponding to the |
| 935 | last *n* positional parameters, or ``None`` if there are no such defaults |
| 936 | defined. |
larryhastings | f36ba12 | 2018-01-28 11:13:09 -0800 | [diff] [blame] | 937 | *kwonlyargs* is a list of keyword-only parameter names in declaration order. |
Nick Coghlan | 3c35fdb | 2016-12-02 20:29:57 +1000 | [diff] [blame] | 938 | *kwonlydefaults* is a dictionary mapping parameter names from *kwonlyargs* |
| 939 | to the default values used if no argument is supplied. |
| 940 | *annotations* is a dictionary mapping parameter names to annotations. |
| 941 | The special key ``"return"`` is used to report the function return value |
| 942 | annotation (if any). |
| 943 | |
| 944 | Note that :func:`signature` and |
| 945 | :ref:`Signature Object <inspect-signature-object>` provide the recommended |
| 946 | API for callable introspection, and support additional behaviours (like |
| 947 | positional-only arguments) that are sometimes encountered in extension module |
| 948 | APIs. This function is retained primarily for use in code that needs to |
| 949 | maintain compatibility with the Python 2 ``inspect`` module API. |
Georg Brandl | 138bcb5 | 2007-09-12 19:04:21 +0000 | [diff] [blame] | 950 | |
Pablo Galindo | d5d2b45 | 2019-04-30 02:01:14 +0100 | [diff] [blame] | 951 | .. deprecated:: 3.8 |
| 952 | Use :func:`signature` and |
| 953 | :ref:`Signature Object <inspect-signature-object>`, which provide a |
| 954 | better introspecting API for callables. |
| 955 | |
Nick Coghlan | 1635578 | 2014-03-08 16:36:37 +1000 | [diff] [blame] | 956 | .. versionchanged:: 3.4 |
| 957 | This function is now based on :func:`signature`, but still ignores |
| 958 | ``__wrapped__`` attributes and includes the already bound first |
| 959 | parameter in the signature output for bound methods. |
| 960 | |
Nick Coghlan | 3c35fdb | 2016-12-02 20:29:57 +1000 | [diff] [blame] | 961 | .. versionchanged:: 3.6 |
| 962 | This method was previously documented as deprecated in favour of |
| 963 | :func:`signature` in Python 3.5, but that decision has been reversed |
| 964 | in order to restore a clearly supported standard interface for |
| 965 | single-source Python 2/3 code migrating away from the legacy |
| 966 | :func:`getargspec` API. |
Yury Selivanov | 3cfec2e | 2015-05-22 11:38:38 -0400 | [diff] [blame] | 967 | |
larryhastings | f36ba12 | 2018-01-28 11:13:09 -0800 | [diff] [blame] | 968 | .. versionchanged:: 3.7 |
| 969 | Python only explicitly guaranteed that it preserved the declaration |
| 970 | order of keyword-only parameters as of version 3.7, although in practice |
| 971 | this order had always been preserved in Python 3. |
| 972 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 973 | |
| 974 | .. function:: getargvalues(frame) |
| 975 | |
Georg Brandl | 3dd3388 | 2009-06-01 17:35:27 +0000 | [diff] [blame] | 976 | Get information about arguments passed into a particular frame. A |
| 977 | :term:`named tuple` ``ArgInfo(args, varargs, keywords, locals)`` is |
Georg Brandl | b30f330 | 2011-01-06 09:23:56 +0000 | [diff] [blame] | 978 | returned. *args* is a list of the argument names. *varargs* and *keywords* |
| 979 | are the names of the ``*`` and ``**`` arguments or ``None``. *locals* is the |
Georg Brandl | c1c4bf8 | 2010-10-15 16:07:41 +0000 | [diff] [blame] | 980 | locals dictionary of the given frame. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 981 | |
Matthias Bussonnier | 0899b98 | 2017-02-21 21:45:51 -0800 | [diff] [blame] | 982 | .. note:: |
| 983 | This function was inadvertently marked as deprecated in Python 3.5. |
Yury Selivanov | 945fff4 | 2015-05-22 16:28:05 -0400 | [diff] [blame] | 984 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 985 | |
Andrew Svetlov | 735d317 | 2012-10-27 00:28:20 +0300 | [diff] [blame] | 986 | .. function:: formatargspec(args[, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, annotations[, formatarg, formatvarargs, formatvarkw, formatvalue, formatreturns, formatannotations]]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 987 | |
Michael Foord | 3af125a | 2012-04-21 18:22:28 +0100 | [diff] [blame] | 988 | Format a pretty argument spec from the values returned by |
Berker Peksag | fa3922c | 2015-07-31 04:11:29 +0300 | [diff] [blame] | 989 | :func:`getfullargspec`. |
Michael Foord | 3af125a | 2012-04-21 18:22:28 +0100 | [diff] [blame] | 990 | |
| 991 | The first seven arguments are (``args``, ``varargs``, ``varkw``, |
Georg Brandl | 8ed75cd | 2014-10-31 10:25:48 +0100 | [diff] [blame] | 992 | ``defaults``, ``kwonlyargs``, ``kwonlydefaults``, ``annotations``). |
Andrew Svetlov | 735d317 | 2012-10-27 00:28:20 +0300 | [diff] [blame] | 993 | |
Georg Brandl | 8ed75cd | 2014-10-31 10:25:48 +0100 | [diff] [blame] | 994 | The other six arguments are functions that are called to turn argument names, |
| 995 | ``*`` argument name, ``**`` argument name, default values, return annotation |
| 996 | and individual annotations into strings, respectively. |
| 997 | |
| 998 | For example: |
| 999 | |
| 1000 | >>> from inspect import formatargspec, getfullargspec |
| 1001 | >>> def f(a: int, b: float): |
| 1002 | ... pass |
| 1003 | ... |
| 1004 | >>> formatargspec(*getfullargspec(f)) |
| 1005 | '(a: int, b: float)' |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1006 | |
Yury Selivanov | 945fff4 | 2015-05-22 16:28:05 -0400 | [diff] [blame] | 1007 | .. deprecated:: 3.5 |
| 1008 | Use :func:`signature` and |
| 1009 | :ref:`Signature Object <inspect-signature-object>`, which provide a |
| 1010 | better introspecting API for callables. |
| 1011 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1012 | |
Georg Brandl | c1c4bf8 | 2010-10-15 16:07:41 +0000 | [diff] [blame] | 1013 | .. function:: formatargvalues(args[, varargs, varkw, locals, formatarg, formatvarargs, formatvarkw, formatvalue]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1014 | |
| 1015 | Format a pretty argument spec from the four values returned by |
| 1016 | :func:`getargvalues`. The format\* arguments are the corresponding optional |
| 1017 | formatting functions that are called to turn names and values into strings. |
| 1018 | |
Matthias Bussonnier | 0899b98 | 2017-02-21 21:45:51 -0800 | [diff] [blame] | 1019 | .. note:: |
| 1020 | This function was inadvertently marked as deprecated in Python 3.5. |
Yury Selivanov | 945fff4 | 2015-05-22 16:28:05 -0400 | [diff] [blame] | 1021 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1022 | |
| 1023 | .. function:: getmro(cls) |
| 1024 | |
| 1025 | Return a tuple of class cls's base classes, including cls, in method resolution |
| 1026 | order. No class appears more than once in this tuple. Note that the method |
| 1027 | resolution order depends on cls's type. Unless a very peculiar user-defined |
| 1028 | metatype is in use, cls will be the first element of the tuple. |
| 1029 | |
| 1030 | |
Benjamin Peterson | 3a990c6 | 2014-01-02 12:22:30 -0600 | [diff] [blame] | 1031 | .. function:: getcallargs(func, *args, **kwds) |
Benjamin Peterson | 25cd7eb | 2010-03-30 18:42:32 +0000 | [diff] [blame] | 1032 | |
| 1033 | Bind the *args* and *kwds* to the argument names of the Python function or |
| 1034 | method *func*, as if it was called with them. For bound methods, bind also the |
| 1035 | first argument (typically named ``self``) to the associated instance. A dict |
| 1036 | is returned, mapping the argument names (including the names of the ``*`` and |
| 1037 | ``**`` arguments, if any) to their values from *args* and *kwds*. In case of |
| 1038 | invoking *func* incorrectly, i.e. whenever ``func(*args, **kwds)`` would raise |
| 1039 | an exception because of incompatible signature, an exception of the same type |
| 1040 | and the same or similar message is raised. For example:: |
| 1041 | |
| 1042 | >>> from inspect import getcallargs |
| 1043 | >>> def f(a, b=1, *pos, **named): |
| 1044 | ... pass |
Andrew Svetlov | e939f38 | 2012-08-09 13:25:32 +0300 | [diff] [blame] | 1045 | >>> getcallargs(f, 1, 2, 3) == {'a': 1, 'named': {}, 'b': 2, 'pos': (3,)} |
| 1046 | True |
| 1047 | >>> getcallargs(f, a=2, x=4) == {'a': 2, 'named': {'x': 4}, 'b': 1, 'pos': ()} |
| 1048 | True |
Benjamin Peterson | 25cd7eb | 2010-03-30 18:42:32 +0000 | [diff] [blame] | 1049 | >>> getcallargs(f) |
| 1050 | Traceback (most recent call last): |
| 1051 | ... |
Andrew Svetlov | e939f38 | 2012-08-09 13:25:32 +0300 | [diff] [blame] | 1052 | TypeError: f() missing 1 required positional argument: 'a' |
Benjamin Peterson | 25cd7eb | 2010-03-30 18:42:32 +0000 | [diff] [blame] | 1053 | |
| 1054 | .. versionadded:: 3.2 |
| 1055 | |
Yury Selivanov | 3cfec2e | 2015-05-22 11:38:38 -0400 | [diff] [blame] | 1056 | .. deprecated:: 3.5 |
| 1057 | Use :meth:`Signature.bind` and :meth:`Signature.bind_partial` instead. |
Andrew Svetlov | 4e48bf9 | 2012-08-13 17:10:28 +0300 | [diff] [blame] | 1058 | |
Benjamin Peterson | 25cd7eb | 2010-03-30 18:42:32 +0000 | [diff] [blame] | 1059 | |
Nick Coghlan | 2f92e54 | 2012-06-23 19:39:55 +1000 | [diff] [blame] | 1060 | .. function:: getclosurevars(func) |
| 1061 | |
| 1062 | Get the mapping of external name references in a Python function or |
| 1063 | method *func* to their current values. A |
| 1064 | :term:`named tuple` ``ClosureVars(nonlocals, globals, builtins, unbound)`` |
| 1065 | is returned. *nonlocals* maps referenced names to lexical closure |
| 1066 | variables, *globals* to the function's module globals and *builtins* to |
| 1067 | the builtins visible from the function body. *unbound* is the set of names |
| 1068 | referenced in the function that could not be resolved at all given the |
| 1069 | current module globals and builtins. |
| 1070 | |
| 1071 | :exc:`TypeError` is raised if *func* is not a Python function or method. |
| 1072 | |
| 1073 | .. versionadded:: 3.3 |
| 1074 | |
| 1075 | |
Nick Coghlan | e8c45d6 | 2013-07-28 20:00:01 +1000 | [diff] [blame] | 1076 | .. function:: unwrap(func, *, stop=None) |
| 1077 | |
| 1078 | Get the object wrapped by *func*. It follows the chain of :attr:`__wrapped__` |
| 1079 | attributes returning the last object in the chain. |
| 1080 | |
| 1081 | *stop* is an optional callback accepting an object in the wrapper chain |
| 1082 | as its sole argument that allows the unwrapping to be terminated early if |
| 1083 | the callback returns a true value. If the callback never returns a true |
| 1084 | value, the last object in the chain is returned as usual. For example, |
| 1085 | :func:`signature` uses this to stop unwrapping if any object in the |
| 1086 | chain has a ``__signature__`` attribute defined. |
| 1087 | |
| 1088 | :exc:`ValueError` is raised if a cycle is encountered. |
| 1089 | |
| 1090 | .. versionadded:: 3.4 |
| 1091 | |
| 1092 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1093 | .. _inspect-stack: |
| 1094 | |
| 1095 | The interpreter stack |
| 1096 | --------------------- |
| 1097 | |
Antoine Pitrou | cdcafb7 | 2014-08-24 10:50:28 -0400 | [diff] [blame] | 1098 | When the following functions return "frame records," each record is a |
| 1099 | :term:`named tuple` |
| 1100 | ``FrameInfo(frame, filename, lineno, function, code_context, index)``. |
| 1101 | The tuple contains the frame object, the filename, the line number of the |
| 1102 | current line, |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1103 | the function name, a list of lines of context from the source code, and the |
| 1104 | index of the current line within that list. |
| 1105 | |
Antoine Pitrou | cdcafb7 | 2014-08-24 10:50:28 -0400 | [diff] [blame] | 1106 | .. versionchanged:: 3.5 |
| 1107 | Return a named tuple instead of a tuple. |
| 1108 | |
Georg Brandl | e720c0a | 2009-04-27 16:20:50 +0000 | [diff] [blame] | 1109 | .. note:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1110 | |
| 1111 | Keeping references to frame objects, as found in the first element of the frame |
| 1112 | records these functions return, can cause your program to create reference |
| 1113 | cycles. Once a reference cycle has been created, the lifespan of all objects |
| 1114 | which can be accessed from the objects which form the cycle can become much |
| 1115 | longer even if Python's optional cycle detector is enabled. If such cycles must |
| 1116 | be created, it is important to ensure they are explicitly broken to avoid the |
| 1117 | delayed destruction of objects and increased memory consumption which occurs. |
| 1118 | |
| 1119 | Though the cycle detector will catch these, destruction of the frames (and local |
| 1120 | variables) can be made deterministic by removing the cycle in a |
| 1121 | :keyword:`finally` clause. This is also important if the cycle detector was |
| 1122 | disabled when Python was compiled or using :func:`gc.disable`. For example:: |
| 1123 | |
| 1124 | def handle_stackframe_without_leak(): |
| 1125 | frame = inspect.currentframe() |
| 1126 | try: |
| 1127 | # do something with the frame |
| 1128 | finally: |
| 1129 | del frame |
| 1130 | |
Antoine Pitrou | 58720d6 | 2013-08-05 23:26:40 +0200 | [diff] [blame] | 1131 | If you want to keep the frame around (for example to print a traceback |
| 1132 | later), you can also break reference cycles by using the |
| 1133 | :meth:`frame.clear` method. |
| 1134 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1135 | The optional *context* argument supported by most of these functions specifies |
| 1136 | the number of lines of context to return, which are centered around the current |
| 1137 | line. |
| 1138 | |
| 1139 | |
Georg Brandl | 3dd3388 | 2009-06-01 17:35:27 +0000 | [diff] [blame] | 1140 | .. function:: getframeinfo(frame, context=1) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1141 | |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 1142 | Get information about a frame or traceback object. A :term:`named tuple` |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 1143 | ``Traceback(filename, lineno, function, code_context, index)`` is returned. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1144 | |
| 1145 | |
Georg Brandl | 3dd3388 | 2009-06-01 17:35:27 +0000 | [diff] [blame] | 1146 | .. function:: getouterframes(frame, context=1) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1147 | |
| 1148 | Get a list of frame records for a frame and all outer frames. These frames |
| 1149 | represent the calls that lead to the creation of *frame*. The first entry in the |
| 1150 | returned list represents *frame*; the last entry represents the outermost call |
| 1151 | on *frame*'s stack. |
| 1152 | |
Yury Selivanov | 100fc3f | 2015-09-08 22:40:30 -0400 | [diff] [blame] | 1153 | .. versionchanged:: 3.5 |
| 1154 | A list of :term:`named tuples <named tuple>` |
| 1155 | ``FrameInfo(frame, filename, lineno, function, code_context, index)`` |
| 1156 | is returned. |
| 1157 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1158 | |
Georg Brandl | 3dd3388 | 2009-06-01 17:35:27 +0000 | [diff] [blame] | 1159 | .. function:: getinnerframes(traceback, context=1) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1160 | |
| 1161 | Get a list of frame records for a traceback's frame and all inner frames. These |
| 1162 | frames represent calls made as a consequence of *frame*. The first entry in the |
| 1163 | list represents *traceback*; the last entry represents where the exception was |
| 1164 | raised. |
| 1165 | |
Yury Selivanov | 100fc3f | 2015-09-08 22:40:30 -0400 | [diff] [blame] | 1166 | .. versionchanged:: 3.5 |
| 1167 | A list of :term:`named tuples <named tuple>` |
| 1168 | ``FrameInfo(frame, filename, lineno, function, code_context, index)`` |
| 1169 | is returned. |
| 1170 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1171 | |
| 1172 | .. function:: currentframe() |
| 1173 | |
| 1174 | Return the frame object for the caller's stack frame. |
| 1175 | |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 1176 | .. impl-detail:: |
| 1177 | |
| 1178 | This function relies on Python stack frame support in the interpreter, |
| 1179 | which isn't guaranteed to exist in all implementations of Python. If |
| 1180 | running in an implementation without Python stack frame support this |
| 1181 | function returns ``None``. |
Benjamin Peterson | 4ac9ce4 | 2009-10-04 14:49:41 +0000 | [diff] [blame] | 1182 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1183 | |
Georg Brandl | 3dd3388 | 2009-06-01 17:35:27 +0000 | [diff] [blame] | 1184 | .. function:: stack(context=1) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1185 | |
| 1186 | Return a list of frame records for the caller's stack. The first entry in the |
| 1187 | returned list represents the caller; the last entry represents the outermost |
| 1188 | call on the stack. |
| 1189 | |
Yury Selivanov | 100fc3f | 2015-09-08 22:40:30 -0400 | [diff] [blame] | 1190 | .. versionchanged:: 3.5 |
| 1191 | A list of :term:`named tuples <named tuple>` |
| 1192 | ``FrameInfo(frame, filename, lineno, function, code_context, index)`` |
| 1193 | is returned. |
| 1194 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1195 | |
Georg Brandl | 3dd3388 | 2009-06-01 17:35:27 +0000 | [diff] [blame] | 1196 | .. function:: trace(context=1) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1197 | |
| 1198 | Return a list of frame records for the stack between the current frame and the |
| 1199 | frame in which an exception currently being handled was raised in. The first |
| 1200 | entry in the list represents the caller; the last entry represents where the |
| 1201 | exception was raised. |
| 1202 | |
Yury Selivanov | 100fc3f | 2015-09-08 22:40:30 -0400 | [diff] [blame] | 1203 | .. versionchanged:: 3.5 |
| 1204 | A list of :term:`named tuples <named tuple>` |
| 1205 | ``FrameInfo(frame, filename, lineno, function, code_context, index)`` |
| 1206 | is returned. |
| 1207 | |
Michael Foord | 95fc51d | 2010-11-20 15:07:30 +0000 | [diff] [blame] | 1208 | |
| 1209 | Fetching attributes statically |
| 1210 | ------------------------------ |
| 1211 | |
| 1212 | Both :func:`getattr` and :func:`hasattr` can trigger code execution when |
| 1213 | fetching or checking for the existence of attributes. Descriptors, like |
| 1214 | properties, will be invoked and :meth:`__getattr__` and :meth:`__getattribute__` |
| 1215 | may be called. |
| 1216 | |
| 1217 | For cases where you want passive introspection, like documentation tools, this |
Éric Araujo | 941afed | 2011-09-01 02:47:34 +0200 | [diff] [blame] | 1218 | can be inconvenient. :func:`getattr_static` has the same signature as :func:`getattr` |
Michael Foord | 95fc51d | 2010-11-20 15:07:30 +0000 | [diff] [blame] | 1219 | but avoids executing code when it fetches attributes. |
| 1220 | |
| 1221 | .. function:: getattr_static(obj, attr, default=None) |
| 1222 | |
| 1223 | Retrieve attributes without triggering dynamic lookup via the |
Éric Araujo | 941afed | 2011-09-01 02:47:34 +0200 | [diff] [blame] | 1224 | descriptor protocol, :meth:`__getattr__` or :meth:`__getattribute__`. |
Michael Foord | 95fc51d | 2010-11-20 15:07:30 +0000 | [diff] [blame] | 1225 | |
| 1226 | Note: this function may not be able to retrieve all attributes |
| 1227 | that getattr can fetch (like dynamically created attributes) |
| 1228 | and may find attributes that getattr can't (like descriptors |
| 1229 | that raise AttributeError). It can also return descriptors objects |
| 1230 | instead of instance members. |
| 1231 | |
Serhiy Storchaka | bfdcd43 | 2013-10-13 23:09:14 +0300 | [diff] [blame] | 1232 | If the instance :attr:`~object.__dict__` is shadowed by another member (for |
| 1233 | example a property) then this function will be unable to find instance |
| 1234 | members. |
Nick Coghlan | 2dad5ca | 2010-11-21 03:55:53 +0000 | [diff] [blame] | 1235 | |
Michael Foord | dcebe0f | 2011-03-15 19:20:44 -0400 | [diff] [blame] | 1236 | .. versionadded:: 3.2 |
Michael Foord | 95fc51d | 2010-11-20 15:07:30 +0000 | [diff] [blame] | 1237 | |
Éric Araujo | 941afed | 2011-09-01 02:47:34 +0200 | [diff] [blame] | 1238 | :func:`getattr_static` does not resolve descriptors, for example slot descriptors or |
Michael Foord | e516265 | 2010-11-20 16:40:44 +0000 | [diff] [blame] | 1239 | getset descriptors on objects implemented in C. The descriptor object |
Michael Foord | 95fc51d | 2010-11-20 15:07:30 +0000 | [diff] [blame] | 1240 | is returned instead of the underlying attribute. |
| 1241 | |
| 1242 | You can handle these with code like the following. Note that |
| 1243 | for arbitrary getset descriptors invoking these may trigger |
| 1244 | code execution:: |
| 1245 | |
| 1246 | # example code for resolving the builtin descriptor types |
Éric Araujo | 28053fb | 2010-11-22 03:09:19 +0000 | [diff] [blame] | 1247 | class _foo: |
Michael Foord | 95fc51d | 2010-11-20 15:07:30 +0000 | [diff] [blame] | 1248 | __slots__ = ['foo'] |
| 1249 | |
| 1250 | slot_descriptor = type(_foo.foo) |
| 1251 | getset_descriptor = type(type(open(__file__)).name) |
| 1252 | wrapper_descriptor = type(str.__dict__['__add__']) |
| 1253 | descriptor_types = (slot_descriptor, getset_descriptor, wrapper_descriptor) |
| 1254 | |
| 1255 | result = getattr_static(some_object, 'foo') |
| 1256 | if type(result) in descriptor_types: |
| 1257 | try: |
| 1258 | result = result.__get__() |
| 1259 | except AttributeError: |
| 1260 | # descriptors can raise AttributeError to |
| 1261 | # indicate there is no underlying value |
| 1262 | # in which case the descriptor itself will |
| 1263 | # have to do |
| 1264 | pass |
Nick Coghlan | e0f0465 | 2010-11-21 03:44:04 +0000 | [diff] [blame] | 1265 | |
Nick Coghlan | 2dad5ca | 2010-11-21 03:55:53 +0000 | [diff] [blame] | 1266 | |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 1267 | Current State of Generators and Coroutines |
| 1268 | ------------------------------------------ |
Nick Coghlan | e0f0465 | 2010-11-21 03:44:04 +0000 | [diff] [blame] | 1269 | |
| 1270 | When implementing coroutine schedulers and for other advanced uses of |
| 1271 | generators, it is useful to determine whether a generator is currently |
| 1272 | executing, is waiting to start or resume or execution, or has already |
Raymond Hettinger | 48f3bd3 | 2010-12-16 00:30:53 +0000 | [diff] [blame] | 1273 | terminated. :func:`getgeneratorstate` allows the current state of a |
Nick Coghlan | e0f0465 | 2010-11-21 03:44:04 +0000 | [diff] [blame] | 1274 | generator to be determined easily. |
| 1275 | |
| 1276 | .. function:: getgeneratorstate(generator) |
| 1277 | |
Raymond Hettinger | 48f3bd3 | 2010-12-16 00:30:53 +0000 | [diff] [blame] | 1278 | Get current state of a generator-iterator. |
Nick Coghlan | e0f0465 | 2010-11-21 03:44:04 +0000 | [diff] [blame] | 1279 | |
Raymond Hettinger | 48f3bd3 | 2010-12-16 00:30:53 +0000 | [diff] [blame] | 1280 | Possible states are: |
Raymond Hettinger | a275c98 | 2011-01-20 04:03:19 +0000 | [diff] [blame] | 1281 | * GEN_CREATED: Waiting to start execution. |
| 1282 | * GEN_RUNNING: Currently being executed by the interpreter. |
| 1283 | * GEN_SUSPENDED: Currently suspended at a yield expression. |
| 1284 | * GEN_CLOSED: Execution has completed. |
Nick Coghlan | e0f0465 | 2010-11-21 03:44:04 +0000 | [diff] [blame] | 1285 | |
Nick Coghlan | 2dad5ca | 2010-11-21 03:55:53 +0000 | [diff] [blame] | 1286 | .. versionadded:: 3.2 |
Nick Coghlan | 04e2e3f | 2012-06-23 19:52:05 +1000 | [diff] [blame] | 1287 | |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 1288 | .. function:: getcoroutinestate(coroutine) |
| 1289 | |
| 1290 | Get current state of a coroutine object. The function is intended to be |
| 1291 | used with coroutine objects created by :keyword:`async def` functions, but |
| 1292 | will accept any coroutine-like object that has ``cr_running`` and |
| 1293 | ``cr_frame`` attributes. |
| 1294 | |
| 1295 | Possible states are: |
| 1296 | * CORO_CREATED: Waiting to start execution. |
| 1297 | * CORO_RUNNING: Currently being executed by the interpreter. |
| 1298 | * CORO_SUSPENDED: Currently suspended at an await expression. |
| 1299 | * CORO_CLOSED: Execution has completed. |
| 1300 | |
| 1301 | .. versionadded:: 3.5 |
| 1302 | |
Nick Coghlan | 04e2e3f | 2012-06-23 19:52:05 +1000 | [diff] [blame] | 1303 | The current internal state of the generator can also be queried. This is |
| 1304 | mostly useful for testing purposes, to ensure that internal state is being |
| 1305 | updated as expected: |
| 1306 | |
| 1307 | .. function:: getgeneratorlocals(generator) |
| 1308 | |
| 1309 | Get the mapping of live local variables in *generator* to their current |
| 1310 | values. A dictionary is returned that maps from variable names to values. |
| 1311 | This is the equivalent of calling :func:`locals` in the body of the |
| 1312 | generator, and all the same caveats apply. |
| 1313 | |
| 1314 | If *generator* is a :term:`generator` with no currently associated frame, |
| 1315 | then an empty dictionary is returned. :exc:`TypeError` is raised if |
| 1316 | *generator* is not a Python generator object. |
| 1317 | |
| 1318 | .. impl-detail:: |
| 1319 | |
| 1320 | This function relies on the generator exposing a Python stack frame |
| 1321 | for introspection, which isn't guaranteed to be the case in all |
| 1322 | implementations of Python. In such cases, this function will always |
| 1323 | return an empty dictionary. |
| 1324 | |
| 1325 | .. versionadded:: 3.3 |
Nick Coghlan | f94a16b | 2013-09-22 22:46:49 +1000 | [diff] [blame] | 1326 | |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 1327 | .. function:: getcoroutinelocals(coroutine) |
| 1328 | |
| 1329 | This function is analogous to :func:`~inspect.getgeneratorlocals`, but |
| 1330 | works for coroutine objects created by :keyword:`async def` functions. |
| 1331 | |
| 1332 | .. versionadded:: 3.5 |
| 1333 | |
Nick Coghlan | f94a16b | 2013-09-22 22:46:49 +1000 | [diff] [blame] | 1334 | |
Yury Selivanov | ea75a51 | 2016-10-20 13:06:30 -0400 | [diff] [blame] | 1335 | .. _inspect-module-co-flags: |
| 1336 | |
| 1337 | Code Objects Bit Flags |
| 1338 | ---------------------- |
| 1339 | |
| 1340 | Python code objects have a ``co_flags`` attribute, which is a bitmap of |
| 1341 | the following flags: |
| 1342 | |
Xiang Zhang | a6902e6 | 2017-04-13 10:38:28 +0800 | [diff] [blame] | 1343 | .. data:: CO_OPTIMIZED |
| 1344 | |
| 1345 | The code object is optimized, using fast locals. |
| 1346 | |
Yury Selivanov | ea75a51 | 2016-10-20 13:06:30 -0400 | [diff] [blame] | 1347 | .. data:: CO_NEWLOCALS |
| 1348 | |
| 1349 | If set, a new dict will be created for the frame's ``f_locals`` when |
| 1350 | the code object is executed. |
| 1351 | |
| 1352 | .. data:: CO_VARARGS |
| 1353 | |
| 1354 | The code object has a variable positional parameter (``*args``-like). |
| 1355 | |
| 1356 | .. data:: CO_VARKEYWORDS |
| 1357 | |
| 1358 | The code object has a variable keyword parameter (``**kwargs``-like). |
| 1359 | |
Xiang Zhang | a6902e6 | 2017-04-13 10:38:28 +0800 | [diff] [blame] | 1360 | .. data:: CO_NESTED |
| 1361 | |
| 1362 | The flag is set when the code object is a nested function. |
| 1363 | |
Yury Selivanov | ea75a51 | 2016-10-20 13:06:30 -0400 | [diff] [blame] | 1364 | .. data:: CO_GENERATOR |
| 1365 | |
| 1366 | The flag is set when the code object is a generator function, i.e. |
| 1367 | a generator object is returned when the code object is executed. |
| 1368 | |
| 1369 | .. data:: CO_NOFREE |
| 1370 | |
| 1371 | The flag is set if there are no free or cell variables. |
| 1372 | |
| 1373 | .. data:: CO_COROUTINE |
| 1374 | |
Yury Selivanov | b738a1f | 2016-10-20 16:30:51 -0400 | [diff] [blame] | 1375 | The flag is set when the code object is a coroutine function. |
| 1376 | When the code object is executed it returns a coroutine object. |
| 1377 | See :pep:`492` for more details. |
Yury Selivanov | ea75a51 | 2016-10-20 13:06:30 -0400 | [diff] [blame] | 1378 | |
| 1379 | .. versionadded:: 3.5 |
| 1380 | |
| 1381 | .. data:: CO_ITERABLE_COROUTINE |
| 1382 | |
Yury Selivanov | b738a1f | 2016-10-20 16:30:51 -0400 | [diff] [blame] | 1383 | The flag is used to transform generators into generator-based |
| 1384 | coroutines. Generator objects with this flag can be used in |
| 1385 | ``await`` expression, and can ``yield from`` coroutine objects. |
| 1386 | See :pep:`492` for more details. |
Yury Selivanov | ea75a51 | 2016-10-20 13:06:30 -0400 | [diff] [blame] | 1387 | |
| 1388 | .. versionadded:: 3.5 |
| 1389 | |
Yury Selivanov | e20fed9 | 2016-10-20 13:11:34 -0400 | [diff] [blame] | 1390 | .. data:: CO_ASYNC_GENERATOR |
| 1391 | |
Yury Selivanov | b738a1f | 2016-10-20 16:30:51 -0400 | [diff] [blame] | 1392 | The flag is set when the code object is an asynchronous generator |
| 1393 | function. When the code object is executed it returns an |
| 1394 | asynchronous generator object. See :pep:`525` for more details. |
Yury Selivanov | e20fed9 | 2016-10-20 13:11:34 -0400 | [diff] [blame] | 1395 | |
| 1396 | .. versionadded:: 3.6 |
| 1397 | |
Yury Selivanov | ea75a51 | 2016-10-20 13:06:30 -0400 | [diff] [blame] | 1398 | .. note:: |
| 1399 | The flags are specific to CPython, and may not be defined in other |
| 1400 | Python implementations. Furthermore, the flags are an implementation |
| 1401 | detail, and can be removed or deprecated in future Python releases. |
| 1402 | It's recommended to use public APIs from the :mod:`inspect` module |
| 1403 | for any introspection needs. |
| 1404 | |
| 1405 | |
Nick Coghlan | 367df12 | 2013-10-27 01:57:34 +1000 | [diff] [blame] | 1406 | .. _inspect-module-cli: |
| 1407 | |
Nick Coghlan | f94a16b | 2013-09-22 22:46:49 +1000 | [diff] [blame] | 1408 | Command Line Interface |
| 1409 | ---------------------- |
| 1410 | |
| 1411 | The :mod:`inspect` module also provides a basic introspection capability |
| 1412 | from the command line. |
| 1413 | |
| 1414 | .. program:: inspect |
| 1415 | |
| 1416 | By default, accepts the name of a module and prints the source of that |
| 1417 | module. A class or function within the module can be printed instead by |
| 1418 | appended a colon and the qualified name of the target object. |
| 1419 | |
| 1420 | .. cmdoption:: --details |
| 1421 | |
| 1422 | Print information about the specified object rather than the source code |