gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2361 lines
193KB

  1. <!DOCTYPE html>
  2. <html lang="en" data-content_root="../">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
  6. <meta property="og:title" content="inspect — Inspect live objects" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/inspect.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Source code: Lib/inspect.py The inspect module provides several useful functions to help get information about live objects such as modules, classes, methods, functions, tracebacks, frame objects, ..." />
  11. <meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
  12. <meta property="og:image:alt" content="Python documentation" />
  13. <meta name="description" content="Source code: Lib/inspect.py The inspect module provides several useful functions to help get information about live objects such as modules, classes, methods, functions, tracebacks, frame objects, ..." />
  14. <meta property="og:image:width" content="200" />
  15. <meta property="og:image:height" content="200" />
  16. <meta name="theme-color" content="#3776ab" />
  17. <title>inspect — Inspect live objects &#8212; Python 3.12.3 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=80d5e7a1" />
  19. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=bb723527" />
  20. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=b20cc3f5" />
  21. <script src="../_static/documentation_options.js?v=2c828074"></script>
  22. <script src="../_static/doctools.js?v=888ff710"></script>
  23. <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
  24. <script src="../_static/sidebar.js"></script>
  25. <link rel="search" type="application/opensearchdescription+xml"
  26. title="Search within Python 3.12.3 documentation"
  27. href="../_static/opensearch.xml"/>
  28. <link rel="author" title="About these documents" href="../about.html" />
  29. <link rel="index" title="Index" href="../genindex.html" />
  30. <link rel="search" title="Search" href="../search.html" />
  31. <link rel="copyright" title="Copyright" href="../copyright.html" />
  32. <link rel="next" title="site — Site-specific configuration hook" href="site.html" />
  33. <link rel="prev" title="gc — Garbage Collector interface" href="gc.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/inspect.html" />
  35. <style>
  36. @media only screen {
  37. table.full-width-table {
  38. width: 100%;
  39. }
  40. }
  41. </style>
  42. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  43. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  44. <script type="text/javascript" src="../_static/copybutton.js"></script>
  45. <script type="text/javascript" src="../_static/menu.js"></script>
  46. <script type="text/javascript" src="../_static/search-focus.js"></script>
  47. <script type="text/javascript" src="../_static/themetoggle.js"></script>
  48. </head>
  49. <body>
  50. <div class="mobile-nav">
  51. <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
  52. aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
  53. <nav class="nav-content" role="navigation">
  54. <label for="menuToggler" class="toggler__label">
  55. <span></span>
  56. </label>
  57. <span class="nav-items-wrapper">
  58. <a href="https://www.python.org/" class="nav-logo">
  59. <img src="../_static/py.svg" alt="Python logo"/>
  60. </a>
  61. <span class="version_switcher_placeholder"></span>
  62. <form role="search" class="search" action="../search.html" method="get">
  63. <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
  64. <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>
  65. </svg>
  66. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  67. <input type="submit" value="Go"/>
  68. </form>
  69. </span>
  70. </nav>
  71. <div class="menu-wrapper">
  72. <nav class="menu" role="navigation" aria-label="main navigation">
  73. <div class="language_switcher_placeholder"></div>
  74. <label class="theme-selector-label">
  75. Theme
  76. <select class="theme-selector" oninput="activateTheme(this.value)">
  77. <option value="auto" selected>Auto</option>
  78. <option value="light">Light</option>
  79. <option value="dark">Dark</option>
  80. </select>
  81. </label>
  82. <div>
  83. <h3><a href="../contents.html">Table of Contents</a></h3>
  84. <ul>
  85. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">inspect</span></code> — Inspect live objects</a><ul>
  86. <li><a class="reference internal" href="#types-and-members">Types and members</a></li>
  87. <li><a class="reference internal" href="#retrieving-source-code">Retrieving source code</a></li>
  88. <li><a class="reference internal" href="#introspecting-callables-with-the-signature-object">Introspecting callables with the Signature object</a></li>
  89. <li><a class="reference internal" href="#classes-and-functions">Classes and functions</a></li>
  90. <li><a class="reference internal" href="#the-interpreter-stack">The interpreter stack</a></li>
  91. <li><a class="reference internal" href="#fetching-attributes-statically">Fetching attributes statically</a></li>
  92. <li><a class="reference internal" href="#current-state-of-generators-coroutines-and-asynchronous-generators">Current State of Generators, Coroutines, and Asynchronous Generators</a></li>
  93. <li><a class="reference internal" href="#code-objects-bit-flags">Code Objects Bit Flags</a></li>
  94. <li><a class="reference internal" href="#buffer-flags">Buffer flags</a></li>
  95. <li><a class="reference internal" href="#command-line-interface">Command Line Interface</a></li>
  96. </ul>
  97. </li>
  98. </ul>
  99. </div>
  100. <div>
  101. <h4>Previous topic</h4>
  102. <p class="topless"><a href="gc.html"
  103. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gc</span></code> — Garbage Collector interface</a></p>
  104. </div>
  105. <div>
  106. <h4>Next topic</h4>
  107. <p class="topless"><a href="site.html"
  108. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">site</span></code> — Site-specific configuration hook</a></p>
  109. </div>
  110. <div role="note" aria-label="source link">
  111. <h3>This Page</h3>
  112. <ul class="this-page-menu">
  113. <li><a href="../bugs.html">Report a Bug</a></li>
  114. <li>
  115. <a href="https://github.com/python/cpython/blob/main/Doc/library/inspect.rst"
  116. rel="nofollow">Show Source
  117. </a>
  118. </li>
  119. </ul>
  120. </div>
  121. </nav>
  122. </div>
  123. </div>
  124. <div class="related" role="navigation" aria-label="related navigation">
  125. <h3>Navigation</h3>
  126. <ul>
  127. <li class="right" style="margin-right: 10px">
  128. <a href="../genindex.html" title="General Index"
  129. accesskey="I">index</a></li>
  130. <li class="right" >
  131. <a href="../py-modindex.html" title="Python Module Index"
  132. >modules</a> |</li>
  133. <li class="right" >
  134. <a href="site.html" title="site — Site-specific configuration hook"
  135. accesskey="N">next</a> |</li>
  136. <li class="right" >
  137. <a href="gc.html" title="gc — Garbage Collector interface"
  138. accesskey="P">previous</a> |</li>
  139. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  140. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  141. <li class="switchers">
  142. <div class="language_switcher_placeholder"></div>
  143. <div class="version_switcher_placeholder"></div>
  144. </li>
  145. <li>
  146. </li>
  147. <li id="cpython-language-and-version">
  148. <a href="../index.html">3.12.3 Documentation</a> &#187;
  149. </li>
  150. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  151. <li class="nav-item nav-item-2"><a href="python.html" accesskey="U">Python Runtime Services</a> &#187;</li>
  152. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">inspect</span></code> — Inspect live objects</a></li>
  153. <li class="right">
  154. <div class="inline-search" role="search">
  155. <form class="inline-search" action="../search.html" method="get">
  156. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  157. <input type="submit" value="Go" />
  158. </form>
  159. </div>
  160. |
  161. </li>
  162. <li class="right">
  163. <label class="theme-selector-label">
  164. Theme
  165. <select class="theme-selector" oninput="activateTheme(this.value)">
  166. <option value="auto" selected>Auto</option>
  167. <option value="light">Light</option>
  168. <option value="dark">Dark</option>
  169. </select>
  170. </label> |</li>
  171. </ul>
  172. </div>
  173. <div class="document">
  174. <div class="documentwrapper">
  175. <div class="bodywrapper">
  176. <div class="body" role="main">
  177. <section id="inspect-inspect-live-objects">
  178. <h1><a class="reference internal" href="#module-inspect" title="inspect: Extract information and source code from live objects."><code class="xref py py-mod docutils literal notranslate"><span class="pre">inspect</span></code></a> — Inspect live objects<a class="headerlink" href="#inspect-inspect-live-objects" title="Link to this heading">¶</a></h1>
  179. <p id="module-inspect"><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/inspect.py">Lib/inspect.py</a></p>
  180. <hr class="docutils" />
  181. <p>The <a class="reference internal" href="#module-inspect" title="inspect: Extract information and source code from live objects."><code class="xref py py-mod docutils literal notranslate"><span class="pre">inspect</span></code></a> module provides several useful functions to help get
  182. information about live objects such as modules, classes, methods, functions,
  183. tracebacks, frame objects, and code objects. For example, it can help you
  184. examine the contents of a class, retrieve the source code of a method, extract
  185. and format the argument list for a function, or get all the information you need
  186. to display a detailed traceback.</p>
  187. <p>There are four main kinds of services provided by this module: type checking,
  188. getting source code, inspecting classes and functions, and examining the
  189. interpreter stack.</p>
  190. <section id="types-and-members">
  191. <span id="inspect-types"></span><h2>Types and members<a class="headerlink" href="#types-and-members" title="Link to this heading">¶</a></h2>
  192. <p>The <a class="reference internal" href="#inspect.getmembers" title="inspect.getmembers"><code class="xref py py-func docutils literal notranslate"><span class="pre">getmembers()</span></code></a> function retrieves the members of an object such as a
  193. class or module. The functions whose names begin with “is” are mainly
  194. provided as convenient choices for the second argument to <a class="reference internal" href="#inspect.getmembers" title="inspect.getmembers"><code class="xref py py-func docutils literal notranslate"><span class="pre">getmembers()</span></code></a>.
  195. They also help you determine when you can expect to find the following special
  196. attributes (see <a class="reference internal" href="../reference/import.html#import-mod-attrs"><span class="std std-ref">Import-related module attributes</span></a> for module attributes):</p>
  197. <table class="docutils align-default">
  198. <thead>
  199. <tr class="row-odd"><th class="head"><p>Type</p></th>
  200. <th class="head"><p>Attribute</p></th>
  201. <th class="head"><p>Description</p></th>
  202. </tr>
  203. </thead>
  204. <tbody>
  205. <tr class="row-even"><td><p>class</p></td>
  206. <td><p>__doc__</p></td>
  207. <td><p>documentation string</p></td>
  208. </tr>
  209. <tr class="row-odd"><td></td>
  210. <td><p>__name__</p></td>
  211. <td><p>name with which this
  212. class was defined</p></td>
  213. </tr>
  214. <tr class="row-even"><td></td>
  215. <td><p>__qualname__</p></td>
  216. <td><p>qualified name</p></td>
  217. </tr>
  218. <tr class="row-odd"><td></td>
  219. <td><p>__module__</p></td>
  220. <td><p>name of module in which
  221. this class was defined</p></td>
  222. </tr>
  223. <tr class="row-even"><td></td>
  224. <td><p>__type_params__</p></td>
  225. <td><p>A tuple containing the
  226. <a class="reference internal" href="../reference/compound_stmts.html#type-params"><span class="std std-ref">type parameters</span></a> of
  227. a generic class</p></td>
  228. </tr>
  229. <tr class="row-odd"><td><p>method</p></td>
  230. <td><p>__doc__</p></td>
  231. <td><p>documentation string</p></td>
  232. </tr>
  233. <tr class="row-even"><td></td>
  234. <td><p>__name__</p></td>
  235. <td><p>name with which this
  236. method was defined</p></td>
  237. </tr>
  238. <tr class="row-odd"><td></td>
  239. <td><p>__qualname__</p></td>
  240. <td><p>qualified name</p></td>
  241. </tr>
  242. <tr class="row-even"><td></td>
  243. <td><p>__func__</p></td>
  244. <td><p>function object
  245. containing implementation
  246. of method</p></td>
  247. </tr>
  248. <tr class="row-odd"><td></td>
  249. <td><p>__self__</p></td>
  250. <td><p>instance to which this
  251. method is bound, or
  252. <code class="docutils literal notranslate"><span class="pre">None</span></code></p></td>
  253. </tr>
  254. <tr class="row-even"><td></td>
  255. <td><p>__module__</p></td>
  256. <td><p>name of module in which
  257. this method was defined</p></td>
  258. </tr>
  259. <tr class="row-odd"><td><p>function</p></td>
  260. <td><p>__doc__</p></td>
  261. <td><p>documentation string</p></td>
  262. </tr>
  263. <tr class="row-even"><td></td>
  264. <td><p>__name__</p></td>
  265. <td><p>name with which this
  266. function was defined</p></td>
  267. </tr>
  268. <tr class="row-odd"><td></td>
  269. <td><p>__qualname__</p></td>
  270. <td><p>qualified name</p></td>
  271. </tr>
  272. <tr class="row-even"><td></td>
  273. <td><p>__code__</p></td>
  274. <td><p>code object containing
  275. compiled function
  276. <a class="reference internal" href="../glossary.html#term-bytecode"><span class="xref std std-term">bytecode</span></a></p></td>
  277. </tr>
  278. <tr class="row-odd"><td></td>
  279. <td><p>__defaults__</p></td>
  280. <td><p>tuple of any default
  281. values for positional or
  282. keyword parameters</p></td>
  283. </tr>
  284. <tr class="row-even"><td></td>
  285. <td><p>__kwdefaults__</p></td>
  286. <td><p>mapping of any default
  287. values for keyword-only
  288. parameters</p></td>
  289. </tr>
  290. <tr class="row-odd"><td></td>
  291. <td><p>__globals__</p></td>
  292. <td><p>global namespace in which
  293. this function was defined</p></td>
  294. </tr>
  295. <tr class="row-even"><td></td>
  296. <td><p>__builtins__</p></td>
  297. <td><p>builtins namespace</p></td>
  298. </tr>
  299. <tr class="row-odd"><td></td>
  300. <td><p>__annotations__</p></td>
  301. <td><p>mapping of parameters
  302. names to annotations;
  303. <code class="docutils literal notranslate"><span class="pre">&quot;return&quot;</span></code> key is
  304. reserved for return
  305. annotations.</p></td>
  306. </tr>
  307. <tr class="row-even"><td></td>
  308. <td><p>__type_params__</p></td>
  309. <td><p>A tuple containing the
  310. <a class="reference internal" href="../reference/compound_stmts.html#type-params"><span class="std std-ref">type parameters</span></a> of
  311. a generic function</p></td>
  312. </tr>
  313. <tr class="row-odd"><td></td>
  314. <td><p>__module__</p></td>
  315. <td><p>name of module in which
  316. this function was defined</p></td>
  317. </tr>
  318. <tr class="row-even"><td><p>traceback</p></td>
  319. <td><p>tb_frame</p></td>
  320. <td><p>frame object at this
  321. level</p></td>
  322. </tr>
  323. <tr class="row-odd"><td></td>
  324. <td><p>tb_lasti</p></td>
  325. <td><p>index of last attempted
  326. instruction in bytecode</p></td>
  327. </tr>
  328. <tr class="row-even"><td></td>
  329. <td><p>tb_lineno</p></td>
  330. <td><p>current line number in
  331. Python source code</p></td>
  332. </tr>
  333. <tr class="row-odd"><td></td>
  334. <td><p>tb_next</p></td>
  335. <td><p>next inner traceback
  336. object (called by this
  337. level)</p></td>
  338. </tr>
  339. <tr class="row-even"><td><p>frame</p></td>
  340. <td><p>f_back</p></td>
  341. <td><p>next outer frame object
  342. (this frame’s caller)</p></td>
  343. </tr>
  344. <tr class="row-odd"><td></td>
  345. <td><p>f_builtins</p></td>
  346. <td><p>builtins namespace seen
  347. by this frame</p></td>
  348. </tr>
  349. <tr class="row-even"><td></td>
  350. <td><p>f_code</p></td>
  351. <td><p>code object being
  352. executed in this frame</p></td>
  353. </tr>
  354. <tr class="row-odd"><td></td>
  355. <td><p>f_globals</p></td>
  356. <td><p>global namespace seen by
  357. this frame</p></td>
  358. </tr>
  359. <tr class="row-even"><td></td>
  360. <td><p>f_lasti</p></td>
  361. <td><p>index of last attempted
  362. instruction in bytecode</p></td>
  363. </tr>
  364. <tr class="row-odd"><td></td>
  365. <td><p>f_lineno</p></td>
  366. <td><p>current line number in
  367. Python source code</p></td>
  368. </tr>
  369. <tr class="row-even"><td></td>
  370. <td><p>f_locals</p></td>
  371. <td><p>local namespace seen by
  372. this frame</p></td>
  373. </tr>
  374. <tr class="row-odd"><td></td>
  375. <td><p>f_trace</p></td>
  376. <td><p>tracing function for this
  377. frame, or <code class="docutils literal notranslate"><span class="pre">None</span></code></p></td>
  378. </tr>
  379. <tr class="row-even"><td><p>code</p></td>
  380. <td><p>co_argcount</p></td>
  381. <td><p>number of arguments (not
  382. including keyword only
  383. arguments, * or **
  384. args)</p></td>
  385. </tr>
  386. <tr class="row-odd"><td></td>
  387. <td><p>co_code</p></td>
  388. <td><p>string of raw compiled
  389. bytecode</p></td>
  390. </tr>
  391. <tr class="row-even"><td></td>
  392. <td><p>co_cellvars</p></td>
  393. <td><p>tuple of names of cell
  394. variables (referenced by
  395. containing scopes)</p></td>
  396. </tr>
  397. <tr class="row-odd"><td></td>
  398. <td><p>co_consts</p></td>
  399. <td><p>tuple of constants used
  400. in the bytecode</p></td>
  401. </tr>
  402. <tr class="row-even"><td></td>
  403. <td><p>co_filename</p></td>
  404. <td><p>name of file in which
  405. this code object was
  406. created</p></td>
  407. </tr>
  408. <tr class="row-odd"><td></td>
  409. <td><p>co_firstlineno</p></td>
  410. <td><p>number of first line in
  411. Python source code</p></td>
  412. </tr>
  413. <tr class="row-even"><td></td>
  414. <td><p>co_flags</p></td>
  415. <td><p>bitmap of <code class="docutils literal notranslate"><span class="pre">CO_*</span></code> flags,
  416. read more <a class="reference internal" href="#inspect-module-co-flags"><span class="std std-ref">here</span></a></p></td>
  417. </tr>
  418. <tr class="row-odd"><td></td>
  419. <td><p>co_lnotab</p></td>
  420. <td><p>encoded mapping of line
  421. numbers to bytecode
  422. indices</p></td>
  423. </tr>
  424. <tr class="row-even"><td></td>
  425. <td><p>co_freevars</p></td>
  426. <td><p>tuple of names of free
  427. variables (referenced via
  428. a function’s closure)</p></td>
  429. </tr>
  430. <tr class="row-odd"><td></td>
  431. <td><p>co_posonlyargcount</p></td>
  432. <td><p>number of positional only
  433. arguments</p></td>
  434. </tr>
  435. <tr class="row-even"><td></td>
  436. <td><p>co_kwonlyargcount</p></td>
  437. <td><p>number of keyword only
  438. arguments (not including
  439. ** arg)</p></td>
  440. </tr>
  441. <tr class="row-odd"><td></td>
  442. <td><p>co_name</p></td>
  443. <td><p>name with which this code
  444. object was defined</p></td>
  445. </tr>
  446. <tr class="row-even"><td></td>
  447. <td><p>co_qualname</p></td>
  448. <td><p>fully qualified name with
  449. which this code object
  450. was defined</p></td>
  451. </tr>
  452. <tr class="row-odd"><td></td>
  453. <td><p>co_names</p></td>
  454. <td><p>tuple of names other
  455. than arguments and
  456. function locals</p></td>
  457. </tr>
  458. <tr class="row-even"><td></td>
  459. <td><p>co_nlocals</p></td>
  460. <td><p>number of local variables</p></td>
  461. </tr>
  462. <tr class="row-odd"><td></td>
  463. <td><p>co_stacksize</p></td>
  464. <td><p>virtual machine stack
  465. space required</p></td>
  466. </tr>
  467. <tr class="row-even"><td></td>
  468. <td><p>co_varnames</p></td>
  469. <td><p>tuple of names of
  470. arguments and local
  471. variables</p></td>
  472. </tr>
  473. <tr class="row-odd"><td><p>generator</p></td>
  474. <td><p>__name__</p></td>
  475. <td><p>name</p></td>
  476. </tr>
  477. <tr class="row-even"><td></td>
  478. <td><p>__qualname__</p></td>
  479. <td><p>qualified name</p></td>
  480. </tr>
  481. <tr class="row-odd"><td></td>
  482. <td><p>gi_frame</p></td>
  483. <td><p>frame</p></td>
  484. </tr>
  485. <tr class="row-even"><td></td>
  486. <td><p>gi_running</p></td>
  487. <td><p>is the generator running?</p></td>
  488. </tr>
  489. <tr class="row-odd"><td></td>
  490. <td><p>gi_code</p></td>
  491. <td><p>code</p></td>
  492. </tr>
  493. <tr class="row-even"><td></td>
  494. <td><p>gi_yieldfrom</p></td>
  495. <td><p>object being iterated by
  496. <code class="docutils literal notranslate"><span class="pre">yield</span> <span class="pre">from</span></code>, or
  497. <code class="docutils literal notranslate"><span class="pre">None</span></code></p></td>
  498. </tr>
  499. <tr class="row-odd"><td><p>coroutine</p></td>
  500. <td><p>__name__</p></td>
  501. <td><p>name</p></td>
  502. </tr>
  503. <tr class="row-even"><td></td>
  504. <td><p>__qualname__</p></td>
  505. <td><p>qualified name</p></td>
  506. </tr>
  507. <tr class="row-odd"><td></td>
  508. <td><p>cr_await</p></td>
  509. <td><p>object being awaited on,
  510. or <code class="docutils literal notranslate"><span class="pre">None</span></code></p></td>
  511. </tr>
  512. <tr class="row-even"><td></td>
  513. <td><p>cr_frame</p></td>
  514. <td><p>frame</p></td>
  515. </tr>
  516. <tr class="row-odd"><td></td>
  517. <td><p>cr_running</p></td>
  518. <td><p>is the coroutine running?</p></td>
  519. </tr>
  520. <tr class="row-even"><td></td>
  521. <td><p>cr_code</p></td>
  522. <td><p>code</p></td>
  523. </tr>
  524. <tr class="row-odd"><td></td>
  525. <td><p>cr_origin</p></td>
  526. <td><p>where coroutine was
  527. created, or <code class="docutils literal notranslate"><span class="pre">None</span></code>. See
  528. <a class="reference internal" href="sys.html#sys.set_coroutine_origin_tracking_depth" title="sys.set_coroutine_origin_tracking_depth"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.set_coroutine_origin_tracking_depth()</span></code></a></p></td>
  529. </tr>
  530. <tr class="row-even"><td><p>builtin</p></td>
  531. <td><p>__doc__</p></td>
  532. <td><p>documentation string</p></td>
  533. </tr>
  534. <tr class="row-odd"><td></td>
  535. <td><p>__name__</p></td>
  536. <td><p>original name of this
  537. function or method</p></td>
  538. </tr>
  539. <tr class="row-even"><td></td>
  540. <td><p>__qualname__</p></td>
  541. <td><p>qualified name</p></td>
  542. </tr>
  543. <tr class="row-odd"><td></td>
  544. <td><p>__self__</p></td>
  545. <td><p>instance to which a
  546. method is bound, or
  547. <code class="docutils literal notranslate"><span class="pre">None</span></code></p></td>
  548. </tr>
  549. </tbody>
  550. </table>
  551. <div class="versionchanged">
  552. <p><span class="versionmodified changed">Changed in version 3.5: </span>Add <code class="docutils literal notranslate"><span class="pre">__qualname__</span></code> and <code class="docutils literal notranslate"><span class="pre">gi_yieldfrom</span></code> attributes to generators.</p>
  553. <p>The <code class="docutils literal notranslate"><span class="pre">__name__</span></code> attribute of generators is now set from the function
  554. name, instead of the code name, and it can now be modified.</p>
  555. </div>
  556. <div class="versionchanged">
  557. <p><span class="versionmodified changed">Changed in version 3.7: </span>Add <code class="docutils literal notranslate"><span class="pre">cr_origin</span></code> attribute to coroutines.</p>
  558. </div>
  559. <div class="versionchanged">
  560. <p><span class="versionmodified changed">Changed in version 3.10: </span>Add <code class="docutils literal notranslate"><span class="pre">__builtins__</span></code> attribute to functions.</p>
  561. </div>
  562. <dl class="py function">
  563. <dt class="sig sig-object py" id="inspect.getmembers">
  564. <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">getmembers</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">predicate</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#inspect.getmembers" title="Link to this definition">¶</a></dt>
  565. <dd><p>Return all the members of an object in a list of <code class="docutils literal notranslate"><span class="pre">(name,</span> <span class="pre">value)</span></code>
  566. pairs sorted by name. If the optional <em>predicate</em> argument—which will be
  567. called with the <code class="docutils literal notranslate"><span class="pre">value</span></code> object of each member—is supplied, only members
  568. for which the predicate returns a true value are included.</p>
  569. <div class="admonition note">
  570. <p class="admonition-title">Note</p>
  571. <p><a class="reference internal" href="#inspect.getmembers" title="inspect.getmembers"><code class="xref py py-func docutils literal notranslate"><span class="pre">getmembers()</span></code></a> will only return class attributes defined in the
  572. metaclass when the argument is a class and those attributes have been
  573. listed in the metaclass’ custom <a class="reference internal" href="../reference/datamodel.html#object.__dir__" title="object.__dir__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__dir__()</span></code></a>.</p>
  574. </div>
  575. </dd></dl>
  576. <dl class="py function">
  577. <dt class="sig sig-object py" id="inspect.getmembers_static">
  578. <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">getmembers_static</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">predicate</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#inspect.getmembers_static" title="Link to this definition">¶</a></dt>
  579. <dd><p>Return all the members of an object in a list of <code class="docutils literal notranslate"><span class="pre">(name,</span> <span class="pre">value)</span></code>
  580. pairs sorted by name without triggering dynamic lookup via the descriptor
  581. protocol, __getattr__ or __getattribute__. Optionally, only return members
  582. that satisfy a given predicate.</p>
  583. <div class="admonition note">
  584. <p class="admonition-title">Note</p>
  585. <p><a class="reference internal" href="#inspect.getmembers_static" title="inspect.getmembers_static"><code class="xref py py-func docutils literal notranslate"><span class="pre">getmembers_static()</span></code></a> may not be able to retrieve all members
  586. that getmembers can fetch (like dynamically created attributes)
  587. and may find members that getmembers can’t (like descriptors
  588. that raise AttributeError). It can also return descriptor objects
  589. instead of instance members in some cases.</p>
  590. </div>
  591. <div class="versionadded">
  592. <p><span class="versionmodified added">New in version 3.11.</span></p>
  593. </div>
  594. </dd></dl>
  595. <dl class="py function">
  596. <dt class="sig sig-object py" id="inspect.getmodulename">
  597. <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">getmodulename</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.getmodulename" title="Link to this definition">¶</a></dt>
  598. <dd><p>Return the name of the module named by the file <em>path</em>, without including the
  599. names of enclosing packages. The file extension is checked against all of
  600. the entries in <a class="reference internal" href="importlib.html#importlib.machinery.all_suffixes" title="importlib.machinery.all_suffixes"><code class="xref py py-func docutils literal notranslate"><span class="pre">importlib.machinery.all_suffixes()</span></code></a>. If it matches,
  601. the final path component is returned with the extension removed.
  602. Otherwise, <code class="docutils literal notranslate"><span class="pre">None</span></code> is returned.</p>
  603. <p>Note that this function <em>only</em> returns a meaningful name for actual
  604. Python modules - paths that potentially refer to Python packages will
  605. still return <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  606. <div class="versionchanged">
  607. <p><span class="versionmodified changed">Changed in version 3.3: </span>The function is based directly on <a class="reference internal" href="importlib.html#module-importlib" title="importlib: The implementation of the import machinery."><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib</span></code></a>.</p>
  608. </div>
  609. </dd></dl>
  610. <dl class="py function">
  611. <dt class="sig sig-object py" id="inspect.ismodule">
  612. <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">ismodule</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.ismodule" title="Link to this definition">¶</a></dt>
  613. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the object is a module.</p>
  614. </dd></dl>
  615. <dl class="py function">
  616. <dt class="sig sig-object py" id="inspect.isclass">
  617. <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">isclass</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.isclass" title="Link to this definition">¶</a></dt>
  618. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the object is a class, whether built-in or created in Python
  619. code.</p>
  620. </dd></dl>
  621. <dl class="py function">
  622. <dt class="sig sig-object py" id="inspect.ismethod">
  623. <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">ismethod</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.ismethod" title="Link to this definition">¶</a></dt>
  624. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the object is a bound method written in Python.</p>
  625. </dd></dl>
  626. <dl class="py function">
  627. <dt class="sig sig-object py" id="inspect.isfunction">
  628. <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">isfunction</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.isfunction" title="Link to this definition">¶</a></dt>
  629. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the object is a Python function, which includes functions
  630. created by a <a class="reference internal" href="../glossary.html#term-lambda"><span class="xref std std-term">lambda</span></a> expression.</p>
  631. </dd></dl>
  632. <dl class="py function">
  633. <dt class="sig sig-object py" id="inspect.isgeneratorfunction">
  634. <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">isgeneratorfunction</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.isgeneratorfunction" title="Link to this definition">¶</a></dt>
  635. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the object is a Python generator function.</p>
  636. <div class="versionchanged">
  637. <p><span class="versionmodified changed">Changed in version 3.8: </span>Functions wrapped in <a class="reference internal" href="functools.html#functools.partial" title="functools.partial"><code class="xref py py-func docutils literal notranslate"><span class="pre">functools.partial()</span></code></a> now return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the
  638. wrapped function is a Python generator function.</p>
  639. </div>
  640. </dd></dl>
  641. <dl class="py function">
  642. <dt class="sig sig-object py" id="inspect.isgenerator">
  643. <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">isgenerator</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.isgenerator" title="Link to this definition">¶</a></dt>
  644. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the object is a generator.</p>
  645. </dd></dl>
  646. <dl class="py function">
  647. <dt class="sig sig-object py" id="inspect.iscoroutinefunction">
  648. <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">iscoroutinefunction</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.iscoroutinefunction" title="Link to this definition">¶</a></dt>
  649. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the object is a <a class="reference internal" href="../glossary.html#term-coroutine-function"><span class="xref std std-term">coroutine function</span></a> (a function
  650. defined with an <a class="reference internal" href="../reference/compound_stmts.html#async-def"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">async</span> <span class="pre">def</span></code></a> syntax), a <a class="reference internal" href="functools.html#functools.partial" title="functools.partial"><code class="xref py py-func docutils literal notranslate"><span class="pre">functools.partial()</span></code></a>
  651. wrapping a <a class="reference internal" href="../glossary.html#term-coroutine-function"><span class="xref std std-term">coroutine function</span></a>, or a sync function marked with
  652. <a class="reference internal" href="#inspect.markcoroutinefunction" title="inspect.markcoroutinefunction"><code class="xref py py-func docutils literal notranslate"><span class="pre">markcoroutinefunction()</span></code></a>.</p>
  653. <div class="versionadded">
  654. <p><span class="versionmodified added">New in version 3.5.</span></p>
  655. </div>
  656. <div class="versionchanged">
  657. <p><span class="versionmodified changed">Changed in version 3.8: </span>Functions wrapped in <a class="reference internal" href="functools.html#functools.partial" title="functools.partial"><code class="xref py py-func docutils literal notranslate"><span class="pre">functools.partial()</span></code></a> now return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the
  658. wrapped function is a <a class="reference internal" href="../glossary.html#term-coroutine-function"><span class="xref std std-term">coroutine function</span></a>.</p>
  659. </div>
  660. <div class="versionchanged">
  661. <p><span class="versionmodified changed">Changed in version 3.12: </span>Sync functions marked with <a class="reference internal" href="#inspect.markcoroutinefunction" title="inspect.markcoroutinefunction"><code class="xref py py-func docutils literal notranslate"><span class="pre">markcoroutinefunction()</span></code></a> now return
  662. <code class="docutils literal notranslate"><span class="pre">True</span></code>.</p>
  663. </div>
  664. </dd></dl>
  665. <dl class="py function">
  666. <dt class="sig sig-object py" id="inspect.markcoroutinefunction">
  667. <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">markcoroutinefunction</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">func</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.markcoroutinefunction" title="Link to this definition">¶</a></dt>
  668. <dd><p>Decorator to mark a callable as a <a class="reference internal" href="../glossary.html#term-coroutine-function"><span class="xref std std-term">coroutine function</span></a> if it would not
  669. otherwise be detected by <a class="reference internal" href="#inspect.iscoroutinefunction" title="inspect.iscoroutinefunction"><code class="xref py py-func docutils literal notranslate"><span class="pre">iscoroutinefunction()</span></code></a>.</p>
  670. <p>This may be of use for sync functions that return a <a class="reference internal" href="../glossary.html#term-coroutine"><span class="xref std std-term">coroutine</span></a>, if
  671. the function is passed to an API that requires <a class="reference internal" href="#inspect.iscoroutinefunction" title="inspect.iscoroutinefunction"><code class="xref py py-func docutils literal notranslate"><span class="pre">iscoroutinefunction()</span></code></a>.</p>
  672. <p>When possible, using an <a class="reference internal" href="../reference/compound_stmts.html#async-def"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">async</span> <span class="pre">def</span></code></a> function is preferred. Also
  673. acceptable is calling the function and testing the return with
  674. <a class="reference internal" href="#inspect.iscoroutine" title="inspect.iscoroutine"><code class="xref py py-func docutils literal notranslate"><span class="pre">iscoroutine()</span></code></a>.</p>
  675. <div class="versionadded">
  676. <p><span class="versionmodified added">New in version 3.12.</span></p>
  677. </div>
  678. </dd></dl>
  679. <dl class="py function">
  680. <dt class="sig sig-object py" id="inspect.iscoroutine">
  681. <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">iscoroutine</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.iscoroutine" title="Link to this definition">¶</a></dt>
  682. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the object is a <a class="reference internal" href="../glossary.html#term-coroutine"><span class="xref std std-term">coroutine</span></a> created by an
  683. <a class="reference internal" href="../reference/compound_stmts.html#async-def"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">async</span> <span class="pre">def</span></code></a> function.</p>
  684. <div class="versionadded">
  685. <p><span class="versionmodified added">New in version 3.5.</span></p>
  686. </div>
  687. </dd></dl>
  688. <dl class="py function">
  689. <dt class="sig sig-object py" id="inspect.isawaitable">
  690. <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">isawaitable</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.isawaitable" title="Link to this definition">¶</a></dt>
  691. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the object can be used in <a class="reference internal" href="../reference/expressions.html#await"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">await</span></code></a> expression.</p>
  692. <p>Can also be used to distinguish generator-based coroutines from regular
  693. generators:</p>
  694. <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">types</span>
  695. <span class="k">def</span> <span class="nf">gen</span><span class="p">():</span>
  696. <span class="k">yield</span>
  697. <span class="nd">@types</span><span class="o">.</span><span class="n">coroutine</span>
  698. <span class="k">def</span> <span class="nf">gen_coro</span><span class="p">():</span>
  699. <span class="k">yield</span>
  700. <span class="k">assert</span> <span class="ow">not</span> <span class="n">isawaitable</span><span class="p">(</span><span class="n">gen</span><span class="p">())</span>
  701. <span class="k">assert</span> <span class="n">isawaitable</span><span class="p">(</span><span class="n">gen_coro</span><span class="p">())</span>
  702. </pre></div>
  703. </div>
  704. <div class="versionadded">
  705. <p><span class="versionmodified added">New in version 3.5.</span></p>
  706. </div>
  707. </dd></dl>
  708. <dl class="py function">
  709. <dt class="sig sig-object py" id="inspect.isasyncgenfunction">
  710. <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">isasyncgenfunction</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.isasyncgenfunction" title="Link to this definition">¶</a></dt>
  711. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the object is an <a class="reference internal" href="../glossary.html#term-asynchronous-generator"><span class="xref std std-term">asynchronous generator</span></a> function,
  712. for example:</p>
  713. <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">async</span> <span class="k">def</span> <span class="nf">agen</span><span class="p">():</span>
  714. <span class="gp">... </span> <span class="k">yield</span> <span class="mi">1</span>
  715. <span class="gp">...</span>
  716. <span class="gp">&gt;&gt;&gt; </span><span class="n">inspect</span><span class="o">.</span><span class="n">isasyncgenfunction</span><span class="p">(</span><span class="n">agen</span><span class="p">)</span>
  717. <span class="go">True</span>
  718. </pre></div>
  719. </div>
  720. <div class="versionadded">
  721. <p><span class="versionmodified added">New in version 3.6.</span></p>
  722. </div>
  723. <div class="versionchanged">
  724. <p><span class="versionmodified changed">Changed in version 3.8: </span>Functions wrapped in <a class="reference internal" href="functools.html#functools.partial" title="functools.partial"><code class="xref py py-func docutils literal notranslate"><span class="pre">functools.partial()</span></code></a> now return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the
  725. wrapped function is a <a class="reference internal" href="../glossary.html#term-asynchronous-generator"><span class="xref std std-term">asynchronous generator</span></a> function.</p>
  726. </div>
  727. </dd></dl>
  728. <dl class="py function">
  729. <dt class="sig sig-object py" id="inspect.isasyncgen">
  730. <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">isasyncgen</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.isasyncgen" title="Link to this definition">¶</a></dt>
  731. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the object is an <a class="reference internal" href="../glossary.html#term-asynchronous-generator-iterator"><span class="xref std std-term">asynchronous generator iterator</span></a>
  732. created by an <a class="reference internal" href="../glossary.html#term-asynchronous-generator"><span class="xref std std-term">asynchronous generator</span></a> function.</p>
  733. <div class="versionadded">
  734. <p><span class="versionmodified added">New in version 3.6.</span></p>
  735. </div>
  736. </dd></dl>
  737. <dl class="py function">
  738. <dt class="sig sig-object py" id="inspect.istraceback">
  739. <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">istraceback</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.istraceback" title="Link to this definition">¶</a></dt>
  740. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the object is a traceback.</p>
  741. </dd></dl>
  742. <dl class="py function">
  743. <dt class="sig sig-object py" id="inspect.isframe">
  744. <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">isframe</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.isframe" title="Link to this definition">¶</a></dt>
  745. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the object is a frame.</p>
  746. </dd></dl>
  747. <dl class="py function">
  748. <dt class="sig sig-object py" id="inspect.iscode">
  749. <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">iscode</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.iscode" title="Link to this definition">¶</a></dt>
  750. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the object is a code.</p>
  751. </dd></dl>
  752. <dl class="py function">
  753. <dt class="sig sig-object py" id="inspect.isbuiltin">
  754. <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">isbuiltin</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.isbuiltin" title="Link to this definition">¶</a></dt>
  755. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the object is a built-in function or a bound built-in method.</p>
  756. </dd></dl>
  757. <dl class="py function">
  758. <dt class="sig sig-object py" id="inspect.ismethodwrapper">
  759. <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">ismethodwrapper</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.ismethodwrapper" title="Link to this definition">¶</a></dt>
  760. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the type of object is a <a class="reference internal" href="types.html#types.MethodWrapperType" title="types.MethodWrapperType"><code class="xref py py-class docutils literal notranslate"><span class="pre">MethodWrapperType</span></code></a>.</p>
  761. <p>These are instances of <a class="reference internal" href="types.html#types.MethodWrapperType" title="types.MethodWrapperType"><code class="xref py py-class docutils literal notranslate"><span class="pre">MethodWrapperType</span></code></a>, such as <a class="reference internal" href="../reference/datamodel.html#object.__str__" title="object.__str__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__str__()</span></code></a>,
  762. <a class="reference internal" href="../reference/datamodel.html#object.__eq__" title="object.__eq__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__eq__()</span></code></a> and <a class="reference internal" href="../reference/datamodel.html#object.__repr__" title="object.__repr__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__repr__()</span></code></a>.</p>
  763. <div class="versionadded">
  764. <p><span class="versionmodified added">New in version 3.11.</span></p>
  765. </div>
  766. </dd></dl>
  767. <dl class="py function">
  768. <dt class="sig sig-object py" id="inspect.isroutine">
  769. <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">isroutine</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.isroutine" title="Link to this definition">¶</a></dt>
  770. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the object is a user-defined or built-in function or method.</p>
  771. </dd></dl>
  772. <dl class="py function">
  773. <dt class="sig sig-object py" id="inspect.isabstract">
  774. <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">isabstract</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.isabstract" title="Link to this definition">¶</a></dt>
  775. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the object is an abstract base class.</p>
  776. </dd></dl>
  777. <dl class="py function">
  778. <dt class="sig sig-object py" id="inspect.ismethoddescriptor">
  779. <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">ismethoddescriptor</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.ismethoddescriptor" title="Link to this definition">¶</a></dt>
  780. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the object is a method descriptor, but not if
  781. <a class="reference internal" href="#inspect.ismethod" title="inspect.ismethod"><code class="xref py py-func docutils literal notranslate"><span class="pre">ismethod()</span></code></a>, <a class="reference internal" href="#inspect.isclass" title="inspect.isclass"><code class="xref py py-func docutils literal notranslate"><span class="pre">isclass()</span></code></a>, <a class="reference internal" href="#inspect.isfunction" title="inspect.isfunction"><code class="xref py py-func docutils literal notranslate"><span class="pre">isfunction()</span></code></a> or <a class="reference internal" href="#inspect.isbuiltin" title="inspect.isbuiltin"><code class="xref py py-func docutils literal notranslate"><span class="pre">isbuiltin()</span></code></a>
  782. are true.</p>
  783. <p>This, for example, is true of <code class="docutils literal notranslate"><span class="pre">int.__add__</span></code>. An object passing this test
  784. has a <a class="reference internal" href="../reference/datamodel.html#object.__get__" title="object.__get__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__get__()</span></code></a> method but not a <a class="reference internal" href="../reference/datamodel.html#object.__set__" title="object.__set__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__set__()</span></code></a>
  785. method, but beyond that the set of attributes varies. A
  786. <a class="reference internal" href="stdtypes.html#definition.__name__" title="definition.__name__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__name__</span></code></a> attribute is usually
  787. sensible, and <code class="xref py py-attr docutils literal notranslate"><span class="pre">__doc__</span></code> often is.</p>
  788. <p>Methods implemented via descriptors that also pass one of the other tests
  789. return <code class="docutils literal notranslate"><span class="pre">False</span></code> from the <a class="reference internal" href="#inspect.ismethoddescriptor" title="inspect.ismethoddescriptor"><code class="xref py py-func docutils literal notranslate"><span class="pre">ismethoddescriptor()</span></code></a> test, simply because the
  790. other tests promise more – you can, e.g., count on having the
  791. <a class="reference internal" href="../reference/datamodel.html#method.__func__" title="method.__func__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__func__</span></code></a> attribute (etc) when an object passes
  792. <a class="reference internal" href="#inspect.ismethod" title="inspect.ismethod"><code class="xref py py-func docutils literal notranslate"><span class="pre">ismethod()</span></code></a>.</p>
  793. </dd></dl>
  794. <dl class="py function">
  795. <dt class="sig sig-object py" id="inspect.isdatadescriptor">
  796. <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">isdatadescriptor</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.isdatadescriptor" title="Link to this definition">¶</a></dt>
  797. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the object is a data descriptor.</p>
  798. <p>Data descriptors have a <a class="reference internal" href="../reference/datamodel.html#object.__set__" title="object.__set__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__set__</span></code></a> or a <a class="reference internal" href="../reference/datamodel.html#object.__delete__" title="object.__delete__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__delete__</span></code></a> method.
  799. Examples are properties (defined in Python), getsets, and members. The
  800. latter two are defined in C and there are more specific tests available for
  801. those types, which is robust across Python implementations. Typically, data
  802. descriptors will also have <a class="reference internal" href="stdtypes.html#definition.__name__" title="definition.__name__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__name__</span></code></a> and <code class="xref py py-attr docutils literal notranslate"><span class="pre">__doc__</span></code> attributes
  803. (properties, getsets, and members have both of these attributes), but this is
  804. not guaranteed.</p>
  805. </dd></dl>
  806. <dl class="py function">
  807. <dt class="sig sig-object py" id="inspect.isgetsetdescriptor">
  808. <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">isgetsetdescriptor</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.isgetsetdescriptor" title="Link to this definition">¶</a></dt>
  809. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the object is a getset descriptor.</p>
  810. <div class="impl-detail compound">
  811. <p><strong>CPython implementation detail:</strong> getsets are attributes defined in extension modules via
  812. <a class="reference internal" href="../c-api/structures.html#c.PyGetSetDef" title="PyGetSetDef"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyGetSetDef</span></code></a> structures. For Python implementations without such
  813. types, this method will always return <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p>
  814. </div>
  815. </dd></dl>
  816. <dl class="py function">
  817. <dt class="sig sig-object py" id="inspect.ismemberdescriptor">
  818. <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">ismemberdescriptor</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.ismemberdescriptor" title="Link to this definition">¶</a></dt>
  819. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the object is a member descriptor.</p>
  820. <div class="impl-detail compound">
  821. <p><strong>CPython implementation detail:</strong> Member descriptors are attributes defined in extension modules via
  822. <a class="reference internal" href="../c-api/structures.html#c.PyMemberDef" title="PyMemberDef"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyMemberDef</span></code></a> structures. For Python implementations without such
  823. types, this method will always return <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p>
  824. </div>
  825. </dd></dl>
  826. </section>
  827. <section id="retrieving-source-code">
  828. <span id="inspect-source"></span><h2>Retrieving source code<a class="headerlink" href="#retrieving-source-code" title="Link to this heading">¶</a></h2>
  829. <dl class="py function">
  830. <dt class="sig sig-object py" id="inspect.getdoc">
  831. <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">getdoc</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.getdoc" title="Link to this definition">¶</a></dt>
  832. <dd><p>Get the documentation string for an object, cleaned up with <a class="reference internal" href="#inspect.cleandoc" title="inspect.cleandoc"><code class="xref py py-func docutils literal notranslate"><span class="pre">cleandoc()</span></code></a>.
  833. If the documentation string for an object is not provided and the object is
  834. a class, a method, a property or a descriptor, retrieve the documentation
  835. string from the inheritance hierarchy.
  836. Return <code class="docutils literal notranslate"><span class="pre">None</span></code> if the documentation string is invalid or missing.</p>
  837. <div class="versionchanged">
  838. <p><span class="versionmodified changed">Changed in version 3.5: </span>Documentation strings are now inherited if not overridden.</p>
  839. </div>
  840. </dd></dl>
  841. <dl class="py function">
  842. <dt class="sig sig-object py" id="inspect.getcomments">
  843. <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">getcomments</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.getcomments" title="Link to this definition">¶</a></dt>
  844. <dd><p>Return in a single string any lines of comments immediately preceding the
  845. object’s source code (for a class, function, or method), or at the top of the
  846. Python source file (if the object is a module). If the object’s source code
  847. is unavailable, return <code class="docutils literal notranslate"><span class="pre">None</span></code>. This could happen if the object has been
  848. defined in C or the interactive shell.</p>
  849. </dd></dl>
  850. <dl class="py function">
  851. <dt class="sig sig-object py" id="inspect.getfile">
  852. <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">getfile</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.getfile" title="Link to this definition">¶</a></dt>
  853. <dd><p>Return the name of the (text or binary) file in which an object was defined.
  854. This will fail with a <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> if the object is a built-in module,
  855. class, or function.</p>
  856. </dd></dl>
  857. <dl class="py function">
  858. <dt class="sig sig-object py" id="inspect.getmodule">
  859. <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">getmodule</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.getmodule" title="Link to this definition">¶</a></dt>
  860. <dd><p>Try to guess which module an object was defined in. Return <code class="docutils literal notranslate"><span class="pre">None</span></code>
  861. if the module cannot be determined.</p>
  862. </dd></dl>
  863. <dl class="py function">
  864. <dt class="sig sig-object py" id="inspect.getsourcefile">
  865. <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">getsourcefile</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.getsourcefile" title="Link to this definition">¶</a></dt>
  866. <dd><p>Return the name of the Python source file in which an object was defined
  867. or <code class="docutils literal notranslate"><span class="pre">None</span></code> if no way can be identified to get the source. This
  868. will fail with a <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> if the object is a built-in module, class, or
  869. function.</p>
  870. </dd></dl>
  871. <dl class="py function">
  872. <dt class="sig sig-object py" id="inspect.getsourcelines">
  873. <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">getsourcelines</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.getsourcelines" title="Link to this definition">¶</a></dt>
  874. <dd><p>Return a list of source lines and starting line number for an object. The
  875. argument may be a module, class, method, function, traceback, frame, or code
  876. object. The source code is returned as a list of the lines corresponding to the
  877. object and the line number indicates where in the original source file the first
  878. line of code was found. An <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> is raised if the source code cannot
  879. be retrieved.
  880. A <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> is raised if the object is a built-in module, class, or
  881. function.</p>
  882. <div class="versionchanged">
  883. <p><span class="versionmodified changed">Changed in version 3.3: </span><a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> is raised instead of <a class="reference internal" href="exceptions.html#IOError" title="IOError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">IOError</span></code></a>, now an alias of the
  884. former.</p>
  885. </div>
  886. </dd></dl>
  887. <dl class="py function">
  888. <dt class="sig sig-object py" id="inspect.getsource">
  889. <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">getsource</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.getsource" title="Link to this definition">¶</a></dt>
  890. <dd><p>Return the text of the source code for an object. The argument may be a module,
  891. class, method, function, traceback, frame, or code object. The source code is
  892. returned as a single string. An <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> is raised if the source code
  893. cannot be retrieved.
  894. A <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> is raised if the object is a built-in module, class, or
  895. function.</p>
  896. <div class="versionchanged">
  897. <p><span class="versionmodified changed">Changed in version 3.3: </span><a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> is raised instead of <a class="reference internal" href="exceptions.html#IOError" title="IOError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">IOError</span></code></a>, now an alias of the
  898. former.</p>
  899. </div>
  900. </dd></dl>
  901. <dl class="py function">
  902. <dt class="sig sig-object py" id="inspect.cleandoc">
  903. <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">cleandoc</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">doc</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.cleandoc" title="Link to this definition">¶</a></dt>
  904. <dd><p>Clean up indentation from docstrings that are indented to line up with blocks
  905. of code.</p>
  906. <p>All leading whitespace is removed from the first line. Any leading whitespace
  907. that can be uniformly removed from the second line onwards is removed. Empty
  908. lines at the beginning and end are subsequently removed. Also, all tabs are
  909. expanded to spaces.</p>
  910. </dd></dl>
  911. </section>
  912. <section id="introspecting-callables-with-the-signature-object">
  913. <span id="inspect-signature-object"></span><h2>Introspecting callables with the Signature object<a class="headerlink" href="#introspecting-callables-with-the-signature-object" title="Link to this heading">¶</a></h2>
  914. <div class="versionadded">
  915. <p><span class="versionmodified added">New in version 3.3.</span></p>
  916. </div>
  917. <p>The <a class="reference internal" href="#inspect.Signature" title="inspect.Signature"><code class="xref py py-class docutils literal notranslate"><span class="pre">Signature</span></code></a> object represents the call signature of a callable object
  918. and its return annotation. To retrieve a <code class="xref py py-class docutils literal notranslate"><span class="pre">Signature</span></code> object,
  919. use the <code class="xref py py-func docutils literal notranslate"><span class="pre">signature()</span></code>
  920. function.</p>
  921. <dl class="py function">
  922. <dt class="sig sig-object py" id="inspect.signature">
  923. <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">signature</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">callable</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">follow_wrapped</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">globals</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">locals</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">eval_str</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.signature" title="Link to this definition">¶</a></dt>
  924. <dd><p>Return a <a class="reference internal" href="#inspect.Signature" title="inspect.Signature"><code class="xref py py-class docutils literal notranslate"><span class="pre">Signature</span></code></a> object for the given <em>callable</em>:</p>
  925. <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">inspect</span> <span class="kn">import</span> <span class="n">signature</span>
  926. <span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">foo</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="o">*</span><span class="p">,</span> <span class="n">b</span><span class="p">:</span><span class="nb">int</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
  927. <span class="gp">... </span> <span class="k">pass</span>
  928. <span class="gp">&gt;&gt;&gt; </span><span class="n">sig</span> <span class="o">=</span> <span class="n">signature</span><span class="p">(</span><span class="n">foo</span><span class="p">)</span>
  929. <span class="gp">&gt;&gt;&gt; </span><span class="nb">str</span><span class="p">(</span><span class="n">sig</span><span class="p">)</span>
  930. <span class="go">&#39;(a, *, b: int, **kwargs)&#39;</span>
  931. <span class="gp">&gt;&gt;&gt; </span><span class="nb">str</span><span class="p">(</span><span class="n">sig</span><span class="o">.</span><span class="n">parameters</span><span class="p">[</span><span class="s1">&#39;b&#39;</span><span class="p">])</span>
  932. <span class="go">&#39;b: int&#39;</span>
  933. <span class="gp">&gt;&gt;&gt; </span><span class="n">sig</span><span class="o">.</span><span class="n">parameters</span><span class="p">[</span><span class="s1">&#39;b&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">annotation</span>
  934. <span class="go">&lt;class &#39;int&#39;&gt;</span>
  935. </pre></div>
  936. </div>
  937. <p>Accepts a wide range of Python callables, from plain functions and classes to
  938. <a class="reference internal" href="functools.html#functools.partial" title="functools.partial"><code class="xref py py-func docutils literal notranslate"><span class="pre">functools.partial()</span></code></a> objects.</p>
  939. <p>For objects defined in modules using stringized annotations
  940. (<code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre">__future__</span> <span class="pre">import</span> <span class="pre">annotations</span></code>), <a class="reference internal" href="#inspect.signature" title="inspect.signature"><code class="xref py py-func docutils literal notranslate"><span class="pre">signature()</span></code></a> will
  941. attempt to automatically un-stringize the annotations using
  942. <a class="reference internal" href="#inspect.get_annotations" title="inspect.get_annotations"><code class="xref py py-func docutils literal notranslate"><span class="pre">get_annotations()</span></code></a>. The
  943. <em>globals</em>, <em>locals</em>, and <em>eval_str</em> parameters are passed
  944. into <a class="reference internal" href="#inspect.get_annotations" title="inspect.get_annotations"><code class="xref py py-func docutils literal notranslate"><span class="pre">get_annotations()</span></code></a> when resolving the
  945. annotations; see the documentation for <a class="reference internal" href="#inspect.get_annotations" title="inspect.get_annotations"><code class="xref py py-func docutils literal notranslate"><span class="pre">get_annotations()</span></code></a>
  946. for instructions on how to use these parameters.</p>
  947. <p>Raises <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> if no signature can be provided, and
  948. <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> if that type of object is not supported. Also,
  949. if the annotations are stringized, and <em>eval_str</em> is not false,
  950. the <code class="docutils literal notranslate"><span class="pre">eval()</span></code> call(s) to un-stringize the annotations in <a class="reference internal" href="#inspect.get_annotations" title="inspect.get_annotations"><code class="xref py py-func docutils literal notranslate"><span class="pre">get_annotations()</span></code></a>
  951. could potentially raise any kind of exception.</p>
  952. <p>A slash(/) in the signature of a function denotes that the parameters prior
  953. to it are positional-only. For more info, see
  954. <a class="reference internal" href="../faq/programming.html#faq-positional-only-arguments"><span class="std std-ref">the FAQ entry on positional-only parameters</span></a>.</p>
  955. <div class="versionchanged">
  956. <p><span class="versionmodified changed">Changed in version 3.5: </span>The <em>follow_wrapped</em> parameter was added.
  957. Pass <code class="docutils literal notranslate"><span class="pre">False</span></code> to get a signature of
  958. <em>callable</em> specifically (<code class="docutils literal notranslate"><span class="pre">callable.__wrapped__</span></code> will not be used to
  959. unwrap decorated callables.)</p>
  960. </div>
  961. <div class="versionchanged">
  962. <p><span class="versionmodified changed">Changed in version 3.10: </span>The <em>globals</em>, <em>locals</em>, and <em>eval_str</em> parameters were added.</p>
  963. </div>
  964. <div class="admonition note">
  965. <p class="admonition-title">Note</p>
  966. <p>Some callables may not be introspectable in certain implementations of
  967. Python. For example, in CPython, some built-in functions defined in
  968. C provide no metadata about their arguments.</p>
  969. </div>
  970. <div class="impl-detail compound">
  971. <p><strong>CPython implementation detail:</strong> If the passed object has a <code class="xref py py-attr docutils literal notranslate"><span class="pre">__signature__</span></code> attribute,
  972. we may use it to create the signature.
  973. The exact semantics are an implementation detail and are subject to
  974. unannounced changes. Consult the source code for current semantics.</p>
  975. </div>
  976. </dd></dl>
  977. <dl class="py class">
  978. <dt class="sig sig-object py" id="inspect.Signature">
  979. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">Signature</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">parameters</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">return_annotation</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">Signature.empty</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.Signature" title="Link to this definition">¶</a></dt>
  980. <dd><p>A <code class="xref py py-class docutils literal notranslate"><span class="pre">Signature</span></code> object represents the call signature of a function
  981. and its return
  982. annotation. For each parameter accepted by the function it stores a
  983. <a class="reference internal" href="#inspect.Parameter" title="inspect.Parameter"><code class="xref py py-class docutils literal notranslate"><span class="pre">Parameter</span></code></a> object in its <a class="reference internal" href="#inspect.Signature.parameters" title="inspect.Signature.parameters"><code class="xref py py-attr docutils literal notranslate"><span class="pre">parameters</span></code></a> collection.</p>
  984. <p>The optional <em>parameters</em> argument is a sequence of <a class="reference internal" href="#inspect.Parameter" title="inspect.Parameter"><code class="xref py py-class docutils literal notranslate"><span class="pre">Parameter</span></code></a>
  985. objects, which is validated to check that there are no parameters with
  986. duplicate names, and that the parameters are in the right order, i.e.
  987. positional-only first, then positional-or-keyword, and that parameters with
  988. defaults follow parameters without defaults.</p>
  989. <p>The optional <em>return_annotation</em> argument can be an arbitrary Python object.
  990. It represents the “return” annotation of the callable.</p>
  991. <p><code class="xref py py-class docutils literal notranslate"><span class="pre">Signature</span></code> objects are <em>immutable</em>. Use <a class="reference internal" href="#inspect.Signature.replace" title="inspect.Signature.replace"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Signature.replace()</span></code></a> to make a
  992. modified copy.</p>
  993. <div class="versionchanged">
  994. <p><span class="versionmodified changed">Changed in version 3.5: </span><code class="xref py py-class docutils literal notranslate"><span class="pre">Signature</span></code> objects are now picklable and <a class="reference internal" href="../glossary.html#term-hashable"><span class="xref std std-term">hashable</span></a>.</p>
  995. </div>
  996. <dl class="py attribute">
  997. <dt class="sig sig-object py" id="inspect.Signature.empty">
  998. <span class="sig-name descname"><span class="pre">empty</span></span><a class="headerlink" href="#inspect.Signature.empty" title="Link to this definition">¶</a></dt>
  999. <dd><p>A special class-level marker to specify absence of a return annotation.</p>
  1000. </dd></dl>
  1001. <dl class="py attribute">
  1002. <dt class="sig sig-object py" id="inspect.Signature.parameters">
  1003. <span class="sig-name descname"><span class="pre">parameters</span></span><a class="headerlink" href="#inspect.Signature.parameters" title="Link to this definition">¶</a></dt>
  1004. <dd><p>An ordered mapping of parameters’ names to the corresponding
  1005. <a class="reference internal" href="#inspect.Parameter" title="inspect.Parameter"><code class="xref py py-class docutils literal notranslate"><span class="pre">Parameter</span></code></a> objects. Parameters appear in strict definition
  1006. order, including keyword-only parameters.</p>
  1007. <div class="versionchanged">
  1008. <p><span class="versionmodified changed">Changed in version 3.7: </span>Python only explicitly guaranteed that it preserved the declaration
  1009. order of keyword-only parameters as of version 3.7, although in practice
  1010. this order had always been preserved in Python 3.</p>
  1011. </div>
  1012. </dd></dl>
  1013. <dl class="py attribute">
  1014. <dt class="sig sig-object py" id="inspect.Signature.return_annotation">
  1015. <span class="sig-name descname"><span class="pre">return_annotation</span></span><a class="headerlink" href="#inspect.Signature.return_annotation" title="Link to this definition">¶</a></dt>
  1016. <dd><p>The “return” annotation for the callable. If the callable has no “return”
  1017. annotation, this attribute is set to <a class="reference internal" href="#inspect.Signature.empty" title="inspect.Signature.empty"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Signature.empty</span></code></a>.</p>
  1018. </dd></dl>
  1019. <dl class="py method">
  1020. <dt class="sig sig-object py" id="inspect.Signature.bind">
  1021. <span class="sig-name descname"><span class="pre">bind</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.Signature.bind" title="Link to this definition">¶</a></dt>
  1022. <dd><p>Create a mapping from positional and keyword arguments to parameters.
  1023. Returns <a class="reference internal" href="#inspect.BoundArguments" title="inspect.BoundArguments"><code class="xref py py-class docutils literal notranslate"><span class="pre">BoundArguments</span></code></a> if <code class="docutils literal notranslate"><span class="pre">*args</span></code> and <code class="docutils literal notranslate"><span class="pre">**kwargs</span></code> match the
  1024. signature, or raises a <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a>.</p>
  1025. </dd></dl>
  1026. <dl class="py method">
  1027. <dt class="sig sig-object py" id="inspect.Signature.bind_partial">
  1028. <span class="sig-name descname"><span class="pre">bind_partial</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.Signature.bind_partial" title="Link to this definition">¶</a></dt>
  1029. <dd><p>Works the same way as <a class="reference internal" href="#inspect.Signature.bind" title="inspect.Signature.bind"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Signature.bind()</span></code></a>, but allows the omission of
  1030. some required arguments (mimics <a class="reference internal" href="functools.html#functools.partial" title="functools.partial"><code class="xref py py-func docutils literal notranslate"><span class="pre">functools.partial()</span></code></a> behavior.)
  1031. Returns <a class="reference internal" href="#inspect.BoundArguments" title="inspect.BoundArguments"><code class="xref py py-class docutils literal notranslate"><span class="pre">BoundArguments</span></code></a>, or raises a <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> if the
  1032. passed arguments do not match the signature.</p>
  1033. </dd></dl>
  1034. <dl class="py method">
  1035. <dt class="sig sig-object py" id="inspect.Signature.replace">
  1036. <span class="sig-name descname"><span class="pre">replace</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="pre">*[,</span> <span class="pre">parameters][,</span> <span class="pre">return_annotation]</span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.Signature.replace" title="Link to this definition">¶</a></dt>
  1037. <dd><p>Create a new <a class="reference internal" href="#inspect.Signature" title="inspect.Signature"><code class="xref py py-class docutils literal notranslate"><span class="pre">Signature</span></code></a> instance based on the instance
  1038. <a class="reference internal" href="#inspect.Signature.replace" title="inspect.Signature.replace"><code class="xref py py-meth docutils literal notranslate"><span class="pre">replace()</span></code></a> was invoked on.
  1039. It is possible to pass different <em>parameters</em> and/or
  1040. <em>return_annotation</em> to override the corresponding properties of the base
  1041. signature. To remove <code class="docutils literal notranslate"><span class="pre">return_annotation</span></code> from the copied
  1042. <code class="xref py py-class docutils literal notranslate"><span class="pre">Signature</span></code>, pass in
  1043. <a class="reference internal" href="#inspect.Signature.empty" title="inspect.Signature.empty"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Signature.empty</span></code></a>.</p>
  1044. <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">test</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">):</span>
  1045. <span class="gp">... </span> <span class="k">pass</span>
  1046. <span class="gp">...</span>
  1047. <span class="gp">&gt;&gt;&gt; </span><span class="n">sig</span> <span class="o">=</span> <span class="n">signature</span><span class="p">(</span><span class="n">test</span><span class="p">)</span>
  1048. <span class="gp">&gt;&gt;&gt; </span><span class="n">new_sig</span> <span class="o">=</span> <span class="n">sig</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">return_annotation</span><span class="o">=</span><span class="s2">&quot;new return anno&quot;</span><span class="p">)</span>
  1049. <span class="gp">&gt;&gt;&gt; </span><span class="nb">str</span><span class="p">(</span><span class="n">new_sig</span><span class="p">)</span>
  1050. <span class="go">&quot;(a, b) -&gt; &#39;new return anno&#39;&quot;</span>
  1051. </pre></div>
  1052. </div>
  1053. </dd></dl>
  1054. <dl class="py method">
  1055. <dt class="sig sig-object py" id="inspect.Signature.from_callable">
  1056. <em class="property"><span class="pre">classmethod</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">from_callable</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">obj</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">follow_wrapped</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">globals</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">locals</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">eval_str</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.Signature.from_callable" title="Link to this definition">¶</a></dt>
  1057. <dd><p>Return a <a class="reference internal" href="#inspect.Signature" title="inspect.Signature"><code class="xref py py-class docutils literal notranslate"><span class="pre">Signature</span></code></a> (or its subclass) object for a given callable
  1058. <em>obj</em>.</p>
  1059. <p>This method simplifies subclassing of <a class="reference internal" href="#inspect.Signature" title="inspect.Signature"><code class="xref py py-class docutils literal notranslate"><span class="pre">Signature</span></code></a>:</p>
  1060. <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">MySignature</span><span class="p">(</span><span class="n">Signature</span><span class="p">):</span>
  1061. <span class="k">pass</span>
  1062. <span class="n">sig</span> <span class="o">=</span> <span class="n">MySignature</span><span class="o">.</span><span class="n">from_callable</span><span class="p">(</span><span class="nb">sum</span><span class="p">)</span>
  1063. <span class="k">assert</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">sig</span><span class="p">,</span> <span class="n">MySignature</span><span class="p">)</span>
  1064. </pre></div>
  1065. </div>
  1066. <p>Its behavior is otherwise identical to that of <a class="reference internal" href="#inspect.signature" title="inspect.signature"><code class="xref py py-func docutils literal notranslate"><span class="pre">signature()</span></code></a>.</p>
  1067. <div class="versionadded">
  1068. <p><span class="versionmodified added">New in version 3.5.</span></p>
  1069. </div>
  1070. <div class="versionchanged">
  1071. <p><span class="versionmodified changed">Changed in version 3.10: </span>The <em>globals</em>, <em>locals</em>, and <em>eval_str</em> parameters were added.</p>
  1072. </div>
  1073. </dd></dl>
  1074. </dd></dl>
  1075. <dl class="py class">
  1076. <dt class="sig sig-object py" id="inspect.Parameter">
  1077. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">Parameter</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">kind</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">default</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">Parameter.empty</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">annotation</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">Parameter.empty</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.Parameter" title="Link to this definition">¶</a></dt>
  1078. <dd><p><code class="xref py py-class docutils literal notranslate"><span class="pre">Parameter</span></code> objects are <em>immutable</em>.
  1079. Instead of modifying a <code class="xref py py-class docutils literal notranslate"><span class="pre">Parameter</span></code> object,
  1080. you can use <a class="reference internal" href="#inspect.Parameter.replace" title="inspect.Parameter.replace"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Parameter.replace()</span></code></a> to create a modified copy.</p>
  1081. <div class="versionchanged">
  1082. <p><span class="versionmodified changed">Changed in version 3.5: </span>Parameter objects are now picklable and <a class="reference internal" href="../glossary.html#term-hashable"><span class="xref std std-term">hashable</span></a>.</p>
  1083. </div>
  1084. <dl class="py attribute">
  1085. <dt class="sig sig-object py" id="inspect.Parameter.empty">
  1086. <span class="sig-name descname"><span class="pre">empty</span></span><a class="headerlink" href="#inspect.Parameter.empty" title="Link to this definition">¶</a></dt>
  1087. <dd><p>A special class-level marker to specify absence of default values and
  1088. annotations.</p>
  1089. </dd></dl>
  1090. <dl class="py attribute">
  1091. <dt class="sig sig-object py" id="inspect.Parameter.name">
  1092. <span class="sig-name descname"><span class="pre">name</span></span><a class="headerlink" href="#inspect.Parameter.name" title="Link to this definition">¶</a></dt>
  1093. <dd><p>The name of the parameter as a string. The name must be a valid
  1094. Python identifier.</p>
  1095. <div class="impl-detail compound">
  1096. <p><strong>CPython implementation detail:</strong> CPython generates implicit parameter names of the form <code class="docutils literal notranslate"><span class="pre">.0</span></code> on the
  1097. code objects used to implement comprehensions and generator
  1098. expressions.</p>
  1099. <div class="versionchanged">
  1100. <p><span class="versionmodified changed">Changed in version 3.6: </span>These parameter names are now exposed by this module as names like
  1101. <code class="docutils literal notranslate"><span class="pre">implicit0</span></code>.</p>
  1102. </div>
  1103. </div>
  1104. </dd></dl>
  1105. <dl class="py attribute">
  1106. <dt class="sig sig-object py" id="inspect.Parameter.default">
  1107. <span class="sig-name descname"><span class="pre">default</span></span><a class="headerlink" href="#inspect.Parameter.default" title="Link to this definition">¶</a></dt>
  1108. <dd><p>The default value for the parameter. If the parameter has no default
  1109. value, this attribute is set to <a class="reference internal" href="#inspect.Parameter.empty" title="inspect.Parameter.empty"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Parameter.empty</span></code></a>.</p>
  1110. </dd></dl>
  1111. <dl class="py attribute">
  1112. <dt class="sig sig-object py" id="inspect.Parameter.annotation">
  1113. <span class="sig-name descname"><span class="pre">annotation</span></span><a class="headerlink" href="#inspect.Parameter.annotation" title="Link to this definition">¶</a></dt>
  1114. <dd><p>The annotation for the parameter. If the parameter has no annotation,
  1115. this attribute is set to <a class="reference internal" href="#inspect.Parameter.empty" title="inspect.Parameter.empty"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Parameter.empty</span></code></a>.</p>
  1116. </dd></dl>
  1117. <dl class="py attribute">
  1118. <dt class="sig sig-object py" id="inspect.Parameter.kind">
  1119. <span class="sig-name descname"><span class="pre">kind</span></span><a class="headerlink" href="#inspect.Parameter.kind" title="Link to this definition">¶</a></dt>
  1120. <dd><p>Describes how argument values are bound to the parameter. The possible
  1121. values are accessible via <a class="reference internal" href="#inspect.Parameter" title="inspect.Parameter"><code class="xref py py-class docutils literal notranslate"><span class="pre">Parameter</span></code></a> (like <code class="docutils literal notranslate"><span class="pre">Parameter.KEYWORD_ONLY</span></code>),
  1122. and support comparison and ordering, in the following order:</p>
  1123. <table class="docutils align-default">
  1124. <thead>
  1125. <tr class="row-odd"><th class="head"><p>Name</p></th>
  1126. <th class="head"><p>Meaning</p></th>
  1127. </tr>
  1128. </thead>
  1129. <tbody>
  1130. <tr class="row-even"><td><p><em>POSITIONAL_ONLY</em></p></td>
  1131. <td><p>Value must be supplied as a positional
  1132. argument. Positional only parameters are
  1133. those which appear before a <code class="docutils literal notranslate"><span class="pre">/</span></code> entry (if
  1134. present) in a Python function definition.</p></td>
  1135. </tr>
  1136. <tr class="row-odd"><td><p><em>POSITIONAL_OR_KEYWORD</em></p></td>
  1137. <td><p>Value may be supplied as either a keyword or
  1138. positional argument (this is the standard
  1139. binding behaviour for functions implemented
  1140. in Python.)</p></td>
  1141. </tr>
  1142. <tr class="row-even"><td><p><em>VAR_POSITIONAL</em></p></td>
  1143. <td><p>A tuple of positional arguments that aren’t
  1144. bound to any other parameter. This
  1145. corresponds to a <code class="docutils literal notranslate"><span class="pre">*args</span></code> parameter in a
  1146. Python function definition.</p></td>
  1147. </tr>
  1148. <tr class="row-odd"><td><p><em>KEYWORD_ONLY</em></p></td>
  1149. <td><p>Value must be supplied as a keyword argument.
  1150. Keyword only parameters are those which
  1151. appear after a <code class="docutils literal notranslate"><span class="pre">*</span></code> or <code class="docutils literal notranslate"><span class="pre">*args</span></code> entry in a
  1152. Python function definition.</p></td>
  1153. </tr>
  1154. <tr class="row-even"><td><p><em>VAR_KEYWORD</em></p></td>
  1155. <td><p>A dict of keyword arguments that aren’t bound
  1156. to any other parameter. This corresponds to a
  1157. <code class="docutils literal notranslate"><span class="pre">**kwargs</span></code> parameter in a Python function
  1158. definition.</p></td>
  1159. </tr>
  1160. </tbody>
  1161. </table>
  1162. <p>Example: print all keyword-only arguments without default values:</p>
  1163. <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">foo</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="o">*</span><span class="p">,</span> <span class="n">c</span><span class="p">,</span> <span class="n">d</span><span class="o">=</span><span class="mi">10</span><span class="p">):</span>
  1164. <span class="gp">... </span> <span class="k">pass</span>
  1165. <span class="gp">&gt;&gt;&gt; </span><span class="n">sig</span> <span class="o">=</span> <span class="n">signature</span><span class="p">(</span><span class="n">foo</span><span class="p">)</span>
  1166. <span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">param</span> <span class="ow">in</span> <span class="n">sig</span><span class="o">.</span><span class="n">parameters</span><span class="o">.</span><span class="n">values</span><span class="p">():</span>
  1167. <span class="gp">... </span> <span class="k">if</span> <span class="p">(</span><span class="n">param</span><span class="o">.</span><span class="n">kind</span> <span class="o">==</span> <span class="n">param</span><span class="o">.</span><span class="n">KEYWORD_ONLY</span> <span class="ow">and</span>
  1168. <span class="gp">... </span> <span class="n">param</span><span class="o">.</span><span class="n">default</span> <span class="ow">is</span> <span class="n">param</span><span class="o">.</span><span class="n">empty</span><span class="p">):</span>
  1169. <span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Parameter:&#39;</span><span class="p">,</span> <span class="n">param</span><span class="p">)</span>
  1170. <span class="go">Parameter: c</span>
  1171. </pre></div>
  1172. </div>
  1173. </dd></dl>
  1174. <dl class="py attribute">
  1175. <dt class="sig sig-object py" id="inspect.Parameter.kind.description">
  1176. <span class="sig-prename descclassname"><span class="pre">kind.</span></span><span class="sig-name descname"><span class="pre">description</span></span><a class="headerlink" href="#inspect.Parameter.kind.description" title="Link to this definition">¶</a></dt>
  1177. <dd><p>Describes a enum value of <a class="reference internal" href="#inspect.Parameter.kind" title="inspect.Parameter.kind"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Parameter.kind</span></code></a>.</p>
  1178. <div class="versionadded">
  1179. <p><span class="versionmodified added">New in version 3.8.</span></p>
  1180. </div>
  1181. <p>Example: print all descriptions of arguments:</p>
  1182. <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">foo</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="o">*</span><span class="p">,</span> <span class="n">c</span><span class="p">,</span> <span class="n">d</span><span class="o">=</span><span class="mi">10</span><span class="p">):</span>
  1183. <span class="gp">... </span> <span class="k">pass</span>
  1184. <span class="gp">&gt;&gt;&gt; </span><span class="n">sig</span> <span class="o">=</span> <span class="n">signature</span><span class="p">(</span><span class="n">foo</span><span class="p">)</span>
  1185. <span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">param</span> <span class="ow">in</span> <span class="n">sig</span><span class="o">.</span><span class="n">parameters</span><span class="o">.</span><span class="n">values</span><span class="p">():</span>
  1186. <span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">param</span><span class="o">.</span><span class="n">kind</span><span class="o">.</span><span class="n">description</span><span class="p">)</span>
  1187. <span class="go">positional or keyword</span>
  1188. <span class="go">positional or keyword</span>
  1189. <span class="go">keyword-only</span>
  1190. <span class="go">keyword-only</span>
  1191. </pre></div>
  1192. </div>
  1193. </dd></dl>
  1194. <dl class="py method">
  1195. <dt class="sig sig-object py" id="inspect.Parameter.replace">
  1196. <span class="sig-name descname"><span class="pre">replace</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="pre">*[,</span> <span class="pre">name][,</span> <span class="pre">kind][,</span> <span class="pre">default][,</span> <span class="pre">annotation]</span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.Parameter.replace" title="Link to this definition">¶</a></dt>
  1197. <dd><p>Create a new <a class="reference internal" href="#inspect.Parameter" title="inspect.Parameter"><code class="xref py py-class docutils literal notranslate"><span class="pre">Parameter</span></code></a> instance based on the instance replaced was invoked
  1198. on. To override a <code class="xref py py-class docutils literal notranslate"><span class="pre">Parameter</span></code> attribute, pass the corresponding
  1199. argument. To remove a default value or/and an annotation from a
  1200. <code class="xref py py-class docutils literal notranslate"><span class="pre">Parameter</span></code>, pass <a class="reference internal" href="#inspect.Parameter.empty" title="inspect.Parameter.empty"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Parameter.empty</span></code></a>.</p>
  1201. <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">inspect</span> <span class="kn">import</span> <span class="n">Parameter</span>
  1202. <span class="gp">&gt;&gt;&gt; </span><span class="n">param</span> <span class="o">=</span> <span class="n">Parameter</span><span class="p">(</span><span class="s1">&#39;foo&#39;</span><span class="p">,</span> <span class="n">Parameter</span><span class="o">.</span><span class="n">KEYWORD_ONLY</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="mi">42</span><span class="p">)</span>
  1203. <span class="gp">&gt;&gt;&gt; </span><span class="nb">str</span><span class="p">(</span><span class="n">param</span><span class="p">)</span>
  1204. <span class="go">&#39;foo=42&#39;</span>
  1205. <span class="gp">&gt;&gt;&gt; </span><span class="nb">str</span><span class="p">(</span><span class="n">param</span><span class="o">.</span><span class="n">replace</span><span class="p">())</span> <span class="c1"># Will create a shallow copy of &#39;param&#39;</span>
  1206. <span class="go">&#39;foo=42&#39;</span>
  1207. <span class="gp">&gt;&gt;&gt; </span><span class="nb">str</span><span class="p">(</span><span class="n">param</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">default</span><span class="o">=</span><span class="n">Parameter</span><span class="o">.</span><span class="n">empty</span><span class="p">,</span> <span class="n">annotation</span><span class="o">=</span><span class="s1">&#39;spam&#39;</span><span class="p">))</span>
  1208. <span class="go">&quot;foo: &#39;spam&#39;&quot;</span>
  1209. </pre></div>
  1210. </div>
  1211. </dd></dl>
  1212. <div class="versionchanged">
  1213. <p><span class="versionmodified changed">Changed in version 3.4: </span>In Python 3.3 <a class="reference internal" href="#inspect.Parameter" title="inspect.Parameter"><code class="xref py py-class docutils literal notranslate"><span class="pre">Parameter</span></code></a> objects were allowed to have <code class="docutils literal notranslate"><span class="pre">name</span></code> set
  1214. to <code class="docutils literal notranslate"><span class="pre">None</span></code> if their <code class="docutils literal notranslate"><span class="pre">kind</span></code> was set to <code class="docutils literal notranslate"><span class="pre">POSITIONAL_ONLY</span></code>.
  1215. This is no longer permitted.</p>
  1216. </div>
  1217. </dd></dl>
  1218. <dl class="py class">
  1219. <dt class="sig sig-object py" id="inspect.BoundArguments">
  1220. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">BoundArguments</span></span><a class="headerlink" href="#inspect.BoundArguments" title="Link to this definition">¶</a></dt>
  1221. <dd><p>Result of a <a class="reference internal" href="#inspect.Signature.bind" title="inspect.Signature.bind"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Signature.bind()</span></code></a> or <a class="reference internal" href="#inspect.Signature.bind_partial" title="inspect.Signature.bind_partial"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Signature.bind_partial()</span></code></a> call.
  1222. Holds the mapping of arguments to the function’s parameters.</p>
  1223. <dl class="py attribute">
  1224. <dt class="sig sig-object py" id="inspect.BoundArguments.arguments">
  1225. <span class="sig-name descname"><span class="pre">arguments</span></span><a class="headerlink" href="#inspect.BoundArguments.arguments" title="Link to this definition">¶</a></dt>
  1226. <dd><p>A mutable mapping of parameters’ names to arguments’ values.
  1227. Contains only explicitly bound arguments. Changes in <a class="reference internal" href="#inspect.BoundArguments.arguments" title="inspect.BoundArguments.arguments"><code class="xref py py-attr docutils literal notranslate"><span class="pre">arguments</span></code></a>
  1228. will reflect in <a class="reference internal" href="#inspect.BoundArguments.args" title="inspect.BoundArguments.args"><code class="xref py py-attr docutils literal notranslate"><span class="pre">args</span></code></a> and <a class="reference internal" href="#inspect.BoundArguments.kwargs" title="inspect.BoundArguments.kwargs"><code class="xref py py-attr docutils literal notranslate"><span class="pre">kwargs</span></code></a>.</p>
  1229. <p>Should be used in conjunction with <a class="reference internal" href="#inspect.Signature.parameters" title="inspect.Signature.parameters"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Signature.parameters</span></code></a> for any
  1230. argument processing purposes.</p>
  1231. <div class="admonition note">
  1232. <p class="admonition-title">Note</p>
  1233. <p>Arguments for which <a class="reference internal" href="#inspect.Signature.bind" title="inspect.Signature.bind"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Signature.bind()</span></code></a> or
  1234. <a class="reference internal" href="#inspect.Signature.bind_partial" title="inspect.Signature.bind_partial"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Signature.bind_partial()</span></code></a> relied on a default value are skipped.
  1235. However, if needed, use <a class="reference internal" href="#inspect.BoundArguments.apply_defaults" title="inspect.BoundArguments.apply_defaults"><code class="xref py py-meth docutils literal notranslate"><span class="pre">BoundArguments.apply_defaults()</span></code></a> to add
  1236. them.</p>
  1237. </div>
  1238. <div class="versionchanged">
  1239. <p><span class="versionmodified changed">Changed in version 3.9: </span><a class="reference internal" href="#inspect.BoundArguments.arguments" title="inspect.BoundArguments.arguments"><code class="xref py py-attr docutils literal notranslate"><span class="pre">arguments</span></code></a> is now of type <a class="reference internal" href="stdtypes.html#dict" title="dict"><code class="xref py py-class docutils literal notranslate"><span class="pre">dict</span></code></a>. Formerly, it was of
  1240. type <a class="reference internal" href="collections.html#collections.OrderedDict" title="collections.OrderedDict"><code class="xref py py-class docutils literal notranslate"><span class="pre">collections.OrderedDict</span></code></a>.</p>
  1241. </div>
  1242. </dd></dl>
  1243. <dl class="py attribute">
  1244. <dt class="sig sig-object py" id="inspect.BoundArguments.args">
  1245. <span class="sig-name descname"><span class="pre">args</span></span><a class="headerlink" href="#inspect.BoundArguments.args" title="Link to this definition">¶</a></dt>
  1246. <dd><p>A tuple of positional arguments values. Dynamically computed from the
  1247. <a class="reference internal" href="#inspect.BoundArguments.arguments" title="inspect.BoundArguments.arguments"><code class="xref py py-attr docutils literal notranslate"><span class="pre">arguments</span></code></a> attribute.</p>
  1248. </dd></dl>
  1249. <dl class="py attribute">
  1250. <dt class="sig sig-object py" id="inspect.BoundArguments.kwargs">
  1251. <span class="sig-name descname"><span class="pre">kwargs</span></span><a class="headerlink" href="#inspect.BoundArguments.kwargs" title="Link to this definition">¶</a></dt>
  1252. <dd><p>A dict of keyword arguments values. Dynamically computed from the
  1253. <a class="reference internal" href="#inspect.BoundArguments.arguments" title="inspect.BoundArguments.arguments"><code class="xref py py-attr docutils literal notranslate"><span class="pre">arguments</span></code></a> attribute.</p>
  1254. </dd></dl>
  1255. <dl class="py attribute">
  1256. <dt class="sig sig-object py" id="inspect.BoundArguments.signature">
  1257. <span class="sig-name descname"><span class="pre">signature</span></span><a class="headerlink" href="#inspect.BoundArguments.signature" title="Link to this definition">¶</a></dt>
  1258. <dd><p>A reference to the parent <a class="reference internal" href="#inspect.Signature" title="inspect.Signature"><code class="xref py py-class docutils literal notranslate"><span class="pre">Signature</span></code></a> object.</p>
  1259. </dd></dl>
  1260. <dl class="py method">
  1261. <dt class="sig sig-object py" id="inspect.BoundArguments.apply_defaults">
  1262. <span class="sig-name descname"><span class="pre">apply_defaults</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#inspect.BoundArguments.apply_defaults" title="Link to this definition">¶</a></dt>
  1263. <dd><p>Set default values for missing arguments.</p>
  1264. <p>For variable-positional arguments (<code class="docutils literal notranslate"><span class="pre">*args</span></code>) the default is an
  1265. empty tuple.</p>
  1266. <p>For variable-keyword arguments (<code class="docutils literal notranslate"><span class="pre">**kwargs</span></code>) the default is an
  1267. empty dict.</p>
  1268. <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">foo</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="o">=</span><span class="s1">&#39;ham&#39;</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">):</span> <span class="k">pass</span>
  1269. <span class="gp">&gt;&gt;&gt; </span><span class="n">ba</span> <span class="o">=</span> <span class="n">inspect</span><span class="o">.</span><span class="n">signature</span><span class="p">(</span><span class="n">foo</span><span class="p">)</span><span class="o">.</span><span class="n">bind</span><span class="p">(</span><span class="s1">&#39;spam&#39;</span><span class="p">)</span>
  1270. <span class="gp">&gt;&gt;&gt; </span><span class="n">ba</span><span class="o">.</span><span class="n">apply_defaults</span><span class="p">()</span>
  1271. <span class="gp">&gt;&gt;&gt; </span><span class="n">ba</span><span class="o">.</span><span class="n">arguments</span>
  1272. <span class="go">{&#39;a&#39;: &#39;spam&#39;, &#39;b&#39;: &#39;ham&#39;, &#39;args&#39;: ()}</span>
  1273. </pre></div>
  1274. </div>
  1275. <div class="versionadded">
  1276. <p><span class="versionmodified added">New in version 3.5.</span></p>
  1277. </div>
  1278. </dd></dl>
  1279. <p>The <a class="reference internal" href="#inspect.BoundArguments.args" title="inspect.BoundArguments.args"><code class="xref py py-attr docutils literal notranslate"><span class="pre">args</span></code></a> and <a class="reference internal" href="#inspect.BoundArguments.kwargs" title="inspect.BoundArguments.kwargs"><code class="xref py py-attr docutils literal notranslate"><span class="pre">kwargs</span></code></a> properties can be used to invoke
  1280. functions:</p>
  1281. <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">test</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="o">*</span><span class="p">,</span> <span class="n">b</span><span class="p">):</span>
  1282. <span class="o">...</span>
  1283. <span class="n">sig</span> <span class="o">=</span> <span class="n">signature</span><span class="p">(</span><span class="n">test</span><span class="p">)</span>
  1284. <span class="n">ba</span> <span class="o">=</span> <span class="n">sig</span><span class="o">.</span><span class="n">bind</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="n">b</span><span class="o">=</span><span class="mi">20</span><span class="p">)</span>
  1285. <span class="n">test</span><span class="p">(</span><span class="o">*</span><span class="n">ba</span><span class="o">.</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">ba</span><span class="o">.</span><span class="n">kwargs</span><span class="p">)</span>
  1286. </pre></div>
  1287. </div>
  1288. </dd></dl>
  1289. <div class="admonition seealso">
  1290. <p class="admonition-title">See also</p>
  1291. <dl class="simple">
  1292. <dt><span class="target" id="index-0"></span><a class="pep reference external" href="https://peps.python.org/pep-0362/"><strong>PEP 362</strong></a> - Function Signature Object.</dt><dd><p>The detailed specification, implementation details and examples.</p>
  1293. </dd>
  1294. </dl>
  1295. </div>
  1296. </section>
  1297. <section id="classes-and-functions">
  1298. <span id="inspect-classes-functions"></span><h2>Classes and functions<a class="headerlink" href="#classes-and-functions" title="Link to this heading">¶</a></h2>
  1299. <dl class="py function">
  1300. <dt class="sig sig-object py" id="inspect.getclasstree">
  1301. <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">getclasstree</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">classes</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">unique</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.getclasstree" title="Link to this definition">¶</a></dt>
  1302. <dd><p>Arrange the given list of classes into a hierarchy of nested lists. Where a
  1303. nested list appears, it contains classes derived from the class whose entry
  1304. immediately precedes the list. Each entry is a 2-tuple containing a class and a
  1305. tuple of its base classes. If the <em>unique</em> argument is true, exactly one entry
  1306. appears in the returned structure for each class in the given list. Otherwise,
  1307. classes using multiple inheritance and their descendants will appear multiple
  1308. times.</p>
  1309. </dd></dl>
  1310. <dl class="py function">
  1311. <dt class="sig sig-object py" id="inspect.getfullargspec">
  1312. <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">getfullargspec</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">func</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.getfullargspec" title="Link to this definition">¶</a></dt>
  1313. <dd><p>Get the names and default values of a Python function’s parameters. A
  1314. <a class="reference internal" href="../glossary.html#term-named-tuple"><span class="xref std std-term">named tuple</span></a> is returned:</p>
  1315. <p><code class="docutils literal notranslate"><span class="pre">FullArgSpec(args,</span> <span class="pre">varargs,</span> <span class="pre">varkw,</span> <span class="pre">defaults,</span> <span class="pre">kwonlyargs,</span> <span class="pre">kwonlydefaults,</span>
  1316. <span class="pre">annotations)</span></code></p>
  1317. <p><em>args</em> is a list of the positional parameter names.
  1318. <em>varargs</em> is the name of the <code class="docutils literal notranslate"><span class="pre">*</span></code> parameter or <code class="docutils literal notranslate"><span class="pre">None</span></code> if arbitrary
  1319. positional arguments are not accepted.
  1320. <em>varkw</em> is the name of the <code class="docutils literal notranslate"><span class="pre">**</span></code> parameter or <code class="docutils literal notranslate"><span class="pre">None</span></code> if arbitrary
  1321. keyword arguments are not accepted.
  1322. <em>defaults</em> is an <em>n</em>-tuple of default argument values corresponding to the
  1323. last <em>n</em> positional parameters, or <code class="docutils literal notranslate"><span class="pre">None</span></code> if there are no such defaults
  1324. defined.
  1325. <em>kwonlyargs</em> is a list of keyword-only parameter names in declaration order.
  1326. <em>kwonlydefaults</em> is a dictionary mapping parameter names from <em>kwonlyargs</em>
  1327. to the default values used if no argument is supplied.
  1328. <em>annotations</em> is a dictionary mapping parameter names to annotations.
  1329. The special key <code class="docutils literal notranslate"><span class="pre">&quot;return&quot;</span></code> is used to report the function return value
  1330. annotation (if any).</p>
  1331. <p>Note that <a class="reference internal" href="#inspect.signature" title="inspect.signature"><code class="xref py py-func docutils literal notranslate"><span class="pre">signature()</span></code></a> and
  1332. <a class="reference internal" href="#inspect-signature-object"><span class="std std-ref">Signature Object</span></a> provide the recommended
  1333. API for callable introspection, and support additional behaviours (like
  1334. positional-only arguments) that are sometimes encountered in extension module
  1335. APIs. This function is retained primarily for use in code that needs to
  1336. maintain compatibility with the Python 2 <code class="docutils literal notranslate"><span class="pre">inspect</span></code> module API.</p>
  1337. <div class="versionchanged">
  1338. <p><span class="versionmodified changed">Changed in version 3.4: </span>This function is now based on <a class="reference internal" href="#inspect.signature" title="inspect.signature"><code class="xref py py-func docutils literal notranslate"><span class="pre">signature()</span></code></a>, but still ignores
  1339. <code class="docutils literal notranslate"><span class="pre">__wrapped__</span></code> attributes and includes the already bound first
  1340. parameter in the signature output for bound methods.</p>
  1341. </div>
  1342. <div class="versionchanged">
  1343. <p><span class="versionmodified changed">Changed in version 3.6: </span>This method was previously documented as deprecated in favour of
  1344. <a class="reference internal" href="#inspect.signature" title="inspect.signature"><code class="xref py py-func docutils literal notranslate"><span class="pre">signature()</span></code></a> in Python 3.5, but that decision has been reversed
  1345. in order to restore a clearly supported standard interface for
  1346. single-source Python 2/3 code migrating away from the legacy
  1347. <code class="xref py py-func docutils literal notranslate"><span class="pre">getargspec()</span></code> API.</p>
  1348. </div>
  1349. <div class="versionchanged">
  1350. <p><span class="versionmodified changed">Changed in version 3.7: </span>Python only explicitly guaranteed that it preserved the declaration
  1351. order of keyword-only parameters as of version 3.7, although in practice
  1352. this order had always been preserved in Python 3.</p>
  1353. </div>
  1354. </dd></dl>
  1355. <dl class="py function">
  1356. <dt class="sig sig-object py" id="inspect.getargvalues">
  1357. <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">getargvalues</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">frame</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.getargvalues" title="Link to this definition">¶</a></dt>
  1358. <dd><p>Get information about arguments passed into a particular frame. A
  1359. <a class="reference internal" href="../glossary.html#term-named-tuple"><span class="xref std std-term">named tuple</span></a> <code class="docutils literal notranslate"><span class="pre">ArgInfo(args,</span> <span class="pre">varargs,</span> <span class="pre">keywords,</span> <span class="pre">locals)</span></code> is
  1360. returned. <em>args</em> is a list of the argument names. <em>varargs</em> and <em>keywords</em>
  1361. are the names of the <code class="docutils literal notranslate"><span class="pre">*</span></code> and <code class="docutils literal notranslate"><span class="pre">**</span></code> arguments or <code class="docutils literal notranslate"><span class="pre">None</span></code>. <em>locals</em> is the
  1362. locals dictionary of the given frame.</p>
  1363. <div class="admonition note">
  1364. <p class="admonition-title">Note</p>
  1365. <p>This function was inadvertently marked as deprecated in Python 3.5.</p>
  1366. </div>
  1367. </dd></dl>
  1368. <dl class="py function">
  1369. <dt class="sig sig-object py" id="inspect.formatargvalues">
  1370. <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">formatargvalues</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">args</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">varargs</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">varkw</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">locals</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">formatarg</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">formatvarargs</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">formatvarkw</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">formatvalue</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#inspect.formatargvalues" title="Link to this definition">¶</a></dt>
  1371. <dd><p>Format a pretty argument spec from the four values returned by
  1372. <a class="reference internal" href="#inspect.getargvalues" title="inspect.getargvalues"><code class="xref py py-func docutils literal notranslate"><span class="pre">getargvalues()</span></code></a>. The format* arguments are the corresponding optional
  1373. formatting functions that are called to turn names and values into strings.</p>
  1374. <div class="admonition note">
  1375. <p class="admonition-title">Note</p>
  1376. <p>This function was inadvertently marked as deprecated in Python 3.5.</p>
  1377. </div>
  1378. </dd></dl>
  1379. <dl class="py function">
  1380. <dt class="sig sig-object py" id="inspect.getmro">
  1381. <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">getmro</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">cls</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.getmro" title="Link to this definition">¶</a></dt>
  1382. <dd><p>Return a tuple of class cls’s base classes, including cls, in method resolution
  1383. order. No class appears more than once in this tuple. Note that the method
  1384. resolution order depends on cls’s type. Unless a very peculiar user-defined
  1385. metatype is in use, cls will be the first element of the tuple.</p>
  1386. </dd></dl>
  1387. <dl class="py function">
  1388. <dt class="sig sig-object py" id="inspect.getcallargs">
  1389. <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">getcallargs</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">func</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwds</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.getcallargs" title="Link to this definition">¶</a></dt>
  1390. <dd><p>Bind the <em>args</em> and <em>kwds</em> to the argument names of the Python function or
  1391. method <em>func</em>, as if it was called with them. For bound methods, bind also the
  1392. first argument (typically named <code class="docutils literal notranslate"><span class="pre">self</span></code>) to the associated instance. A dict
  1393. is returned, mapping the argument names (including the names of the <code class="docutils literal notranslate"><span class="pre">*</span></code> and
  1394. <code class="docutils literal notranslate"><span class="pre">**</span></code> arguments, if any) to their values from <em>args</em> and <em>kwds</em>. In case of
  1395. invoking <em>func</em> incorrectly, i.e. whenever <code class="docutils literal notranslate"><span class="pre">func(*args,</span> <span class="pre">**kwds)</span></code> would raise
  1396. an exception because of incompatible signature, an exception of the same type
  1397. and the same or similar message is raised. For example:</p>
  1398. <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">inspect</span> <span class="kn">import</span> <span class="n">getcallargs</span>
  1399. <span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="o">*</span><span class="n">pos</span><span class="p">,</span> <span class="o">**</span><span class="n">named</span><span class="p">):</span>
  1400. <span class="gp">... </span> <span class="k">pass</span>
  1401. <span class="gp">...</span>
  1402. <span class="gp">&gt;&gt;&gt; </span><span class="n">getcallargs</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span> <span class="o">==</span> <span class="p">{</span><span class="s1">&#39;a&#39;</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span> <span class="s1">&#39;named&#39;</span><span class="p">:</span> <span class="p">{},</span> <span class="s1">&#39;b&#39;</span><span class="p">:</span> <span class="mi">2</span><span class="p">,</span> <span class="s1">&#39;pos&#39;</span><span class="p">:</span> <span class="p">(</span><span class="mi">3</span><span class="p">,)}</span>
  1403. <span class="go">True</span>
  1404. <span class="gp">&gt;&gt;&gt; </span><span class="n">getcallargs</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="n">a</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">x</span><span class="o">=</span><span class="mi">4</span><span class="p">)</span> <span class="o">==</span> <span class="p">{</span><span class="s1">&#39;a&#39;</span><span class="p">:</span> <span class="mi">2</span><span class="p">,</span> <span class="s1">&#39;named&#39;</span><span class="p">:</span> <span class="p">{</span><span class="s1">&#39;x&#39;</span><span class="p">:</span> <span class="mi">4</span><span class="p">},</span> <span class="s1">&#39;b&#39;</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span> <span class="s1">&#39;pos&#39;</span><span class="p">:</span> <span class="p">()}</span>
  1405. <span class="go">True</span>
  1406. <span class="gp">&gt;&gt;&gt; </span><span class="n">getcallargs</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
  1407. <span class="gt">Traceback (most recent call last):</span>
  1408. <span class="c">...</span>
  1409. <span class="gr">TypeError</span>: <span class="n">f() missing 1 required positional argument: &#39;a&#39;</span>
  1410. </pre></div>
  1411. </div>
  1412. <div class="versionadded">
  1413. <p><span class="versionmodified added">New in version 3.2.</span></p>
  1414. </div>
  1415. <div class="deprecated">
  1416. <p><span class="versionmodified deprecated">Deprecated since version 3.5: </span>Use <a class="reference internal" href="#inspect.Signature.bind" title="inspect.Signature.bind"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Signature.bind()</span></code></a> and <a class="reference internal" href="#inspect.Signature.bind_partial" title="inspect.Signature.bind_partial"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Signature.bind_partial()</span></code></a> instead.</p>
  1417. </div>
  1418. </dd></dl>
  1419. <dl class="py function">
  1420. <dt class="sig sig-object py" id="inspect.getclosurevars">
  1421. <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">getclosurevars</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">func</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.getclosurevars" title="Link to this definition">¶</a></dt>
  1422. <dd><p>Get the mapping of external name references in a Python function or
  1423. method <em>func</em> to their current values. A
  1424. <a class="reference internal" href="../glossary.html#term-named-tuple"><span class="xref std std-term">named tuple</span></a> <code class="docutils literal notranslate"><span class="pre">ClosureVars(nonlocals,</span> <span class="pre">globals,</span> <span class="pre">builtins,</span> <span class="pre">unbound)</span></code>
  1425. is returned. <em>nonlocals</em> maps referenced names to lexical closure
  1426. variables, <em>globals</em> to the function’s module globals and <em>builtins</em> to
  1427. the builtins visible from the function body. <em>unbound</em> is the set of names
  1428. referenced in the function that could not be resolved at all given the
  1429. current module globals and builtins.</p>
  1430. <p><a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> is raised if <em>func</em> is not a Python function or method.</p>
  1431. <div class="versionadded">
  1432. <p><span class="versionmodified added">New in version 3.3.</span></p>
  1433. </div>
  1434. </dd></dl>
  1435. <dl class="py function">
  1436. <dt class="sig sig-object py" id="inspect.unwrap">
  1437. <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">unwrap</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">func</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">stop</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.unwrap" title="Link to this definition">¶</a></dt>
  1438. <dd><p>Get the object wrapped by <em>func</em>. It follows the chain of <code class="xref py py-attr docutils literal notranslate"><span class="pre">__wrapped__</span></code>
  1439. attributes returning the last object in the chain.</p>
  1440. <p><em>stop</em> is an optional callback accepting an object in the wrapper chain
  1441. as its sole argument that allows the unwrapping to be terminated early if
  1442. the callback returns a true value. If the callback never returns a true
  1443. value, the last object in the chain is returned as usual. For example,
  1444. <a class="reference internal" href="#inspect.signature" title="inspect.signature"><code class="xref py py-func docutils literal notranslate"><span class="pre">signature()</span></code></a> uses this to stop unwrapping if any object in the
  1445. chain has a <code class="docutils literal notranslate"><span class="pre">__signature__</span></code> attribute defined.</p>
  1446. <p><a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> is raised if a cycle is encountered.</p>
  1447. <div class="versionadded">
  1448. <p><span class="versionmodified added">New in version 3.4.</span></p>
  1449. </div>
  1450. </dd></dl>
  1451. <dl class="py function">
  1452. <dt class="sig sig-object py" id="inspect.get_annotations">
  1453. <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">get_annotations</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">obj</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">globals</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">locals</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">eval_str</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.get_annotations" title="Link to this definition">¶</a></dt>
  1454. <dd><p>Compute the annotations dict for an object.</p>
  1455. <p><code class="docutils literal notranslate"><span class="pre">obj</span></code> may be a callable, class, or module.
  1456. Passing in an object of any other type raises <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a>.</p>
  1457. <p>Returns a dict. <code class="docutils literal notranslate"><span class="pre">get_annotations()</span></code> returns a new dict every time
  1458. it’s called; calling it twice on the same object will return two
  1459. different but equivalent dicts.</p>
  1460. <p>This function handles several details for you:</p>
  1461. <ul class="simple">
  1462. <li><p>If <code class="docutils literal notranslate"><span class="pre">eval_str</span></code> is true, values of type <code class="docutils literal notranslate"><span class="pre">str</span></code> will
  1463. be un-stringized using <a class="reference internal" href="functions.html#eval" title="eval"><code class="xref py py-func docutils literal notranslate"><span class="pre">eval()</span></code></a>. This is intended
  1464. for use with stringized annotations
  1465. (<code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre">__future__</span> <span class="pre">import</span> <span class="pre">annotations</span></code>).</p></li>
  1466. <li><p>If <code class="docutils literal notranslate"><span class="pre">obj</span></code> doesn’t have an annotations dict, returns an
  1467. empty dict. (Functions and methods always have an
  1468. annotations dict; classes, modules, and other types of
  1469. callables may not.)</p></li>
  1470. <li><p>Ignores inherited annotations on classes. If a class
  1471. doesn’t have its own annotations dict, returns an empty dict.</p></li>
  1472. <li><p>All accesses to object members and dict values are done
  1473. using <code class="docutils literal notranslate"><span class="pre">getattr()</span></code> and <code class="docutils literal notranslate"><span class="pre">dict.get()</span></code> for safety.</p></li>
  1474. <li><p>Always, always, always returns a freshly created dict.</p></li>
  1475. </ul>
  1476. <p><code class="docutils literal notranslate"><span class="pre">eval_str</span></code> controls whether or not values of type <code class="docutils literal notranslate"><span class="pre">str</span></code> are replaced
  1477. with the result of calling <a class="reference internal" href="functions.html#eval" title="eval"><code class="xref py py-func docutils literal notranslate"><span class="pre">eval()</span></code></a> on those values:</p>
  1478. <ul class="simple">
  1479. <li><p>If eval_str is true, <a class="reference internal" href="functions.html#eval" title="eval"><code class="xref py py-func docutils literal notranslate"><span class="pre">eval()</span></code></a> is called on values of type <code class="docutils literal notranslate"><span class="pre">str</span></code>.
  1480. (Note that <code class="docutils literal notranslate"><span class="pre">get_annotations</span></code> doesn’t catch exceptions; if <a class="reference internal" href="functions.html#eval" title="eval"><code class="xref py py-func docutils literal notranslate"><span class="pre">eval()</span></code></a>
  1481. raises an exception, it will unwind the stack past the <code class="docutils literal notranslate"><span class="pre">get_annotations</span></code>
  1482. call.)</p></li>
  1483. <li><p>If eval_str is false (the default), values of type <code class="docutils literal notranslate"><span class="pre">str</span></code> are unchanged.</p></li>
  1484. </ul>
  1485. <p><code class="docutils literal notranslate"><span class="pre">globals</span></code> and <code class="docutils literal notranslate"><span class="pre">locals</span></code> are passed in to <a class="reference internal" href="functions.html#eval" title="eval"><code class="xref py py-func docutils literal notranslate"><span class="pre">eval()</span></code></a>; see the documentation
  1486. for <a class="reference internal" href="functions.html#eval" title="eval"><code class="xref py py-func docutils literal notranslate"><span class="pre">eval()</span></code></a> for more information. If <code class="docutils literal notranslate"><span class="pre">globals</span></code> or <code class="docutils literal notranslate"><span class="pre">locals</span></code>
  1487. is <code class="docutils literal notranslate"><span class="pre">None</span></code>, this function may replace that value with a context-specific
  1488. default, contingent on <code class="docutils literal notranslate"><span class="pre">type(obj)</span></code>:</p>
  1489. <ul class="simple">
  1490. <li><p>If <code class="docutils literal notranslate"><span class="pre">obj</span></code> is a module, <code class="docutils literal notranslate"><span class="pre">globals</span></code> defaults to <code class="docutils literal notranslate"><span class="pre">obj.__dict__</span></code>.</p></li>
  1491. <li><p>If <code class="docutils literal notranslate"><span class="pre">obj</span></code> is a class, <code class="docutils literal notranslate"><span class="pre">globals</span></code> defaults to
  1492. <code class="docutils literal notranslate"><span class="pre">sys.modules[obj.__module__].__dict__</span></code> and <code class="docutils literal notranslate"><span class="pre">locals</span></code> defaults
  1493. to the <code class="docutils literal notranslate"><span class="pre">obj</span></code> class namespace.</p></li>
  1494. <li><p>If <code class="docutils literal notranslate"><span class="pre">obj</span></code> is a callable, <code class="docutils literal notranslate"><span class="pre">globals</span></code> defaults to
  1495. <a class="reference internal" href="../reference/datamodel.html#function.__globals__" title="function.__globals__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">obj.__globals__</span></code></a>,
  1496. although if <code class="docutils literal notranslate"><span class="pre">obj</span></code> is a wrapped function (using
  1497. <a class="reference internal" href="functools.html#functools.update_wrapper" title="functools.update_wrapper"><code class="xref py py-func docutils literal notranslate"><span class="pre">functools.update_wrapper()</span></code></a>) it is first unwrapped.</p></li>
  1498. </ul>
  1499. <p>Calling <code class="docutils literal notranslate"><span class="pre">get_annotations</span></code> is best practice for accessing the
  1500. annotations dict of any object. See <a class="reference internal" href="../howto/annotations.html#annotations-howto"><span class="std std-ref">Annotations Best Practices</span></a> for
  1501. more information on annotations best practices.</p>
  1502. <div class="versionadded">
  1503. <p><span class="versionmodified added">New in version 3.10.</span></p>
  1504. </div>
  1505. </dd></dl>
  1506. </section>
  1507. <section id="the-interpreter-stack">
  1508. <span id="inspect-stack"></span><h2>The interpreter stack<a class="headerlink" href="#the-interpreter-stack" title="Link to this heading">¶</a></h2>
  1509. <p>Some of the following functions return
  1510. <a class="reference internal" href="#inspect.FrameInfo" title="inspect.FrameInfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">FrameInfo</span></code></a> objects. For backwards compatibility these objects allow
  1511. tuple-like operations on all attributes except <code class="docutils literal notranslate"><span class="pre">positions</span></code>. This behavior
  1512. is considered deprecated and may be removed in the future.</p>
  1513. <dl class="py class">
  1514. <dt class="sig sig-object py" id="inspect.FrameInfo">
  1515. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">FrameInfo</span></span><a class="headerlink" href="#inspect.FrameInfo" title="Link to this definition">¶</a></dt>
  1516. <dd><dl class="py attribute">
  1517. <dt class="sig sig-object py" id="inspect.FrameInfo.frame">
  1518. <span class="sig-name descname"><span class="pre">frame</span></span><a class="headerlink" href="#inspect.FrameInfo.frame" title="Link to this definition">¶</a></dt>
  1519. <dd><p>The <a class="reference internal" href="../reference/datamodel.html#frame-objects"><span class="std std-ref">frame object</span></a> that the record corresponds to.</p>
  1520. </dd></dl>
  1521. <dl class="py attribute">
  1522. <dt class="sig sig-object py" id="inspect.FrameInfo.filename">
  1523. <span class="sig-name descname"><span class="pre">filename</span></span><a class="headerlink" href="#inspect.FrameInfo.filename" title="Link to this definition">¶</a></dt>
  1524. <dd><p>The file name associated with the code being executed by the frame this record
  1525. corresponds to.</p>
  1526. </dd></dl>
  1527. <dl class="py attribute">
  1528. <dt class="sig sig-object py" id="inspect.FrameInfo.lineno">
  1529. <span class="sig-name descname"><span class="pre">lineno</span></span><a class="headerlink" href="#inspect.FrameInfo.lineno" title="Link to this definition">¶</a></dt>
  1530. <dd><p>The line number of the current line associated with the code being
  1531. executed by the frame this record corresponds to.</p>
  1532. </dd></dl>
  1533. <dl class="py attribute">
  1534. <dt class="sig sig-object py" id="inspect.FrameInfo.function">
  1535. <span class="sig-name descname"><span class="pre">function</span></span><a class="headerlink" href="#inspect.FrameInfo.function" title="Link to this definition">¶</a></dt>
  1536. <dd><p>The function name that is being executed by the frame this record corresponds to.</p>
  1537. </dd></dl>
  1538. <dl class="py attribute">
  1539. <dt class="sig sig-object py" id="inspect.FrameInfo.code_context">
  1540. <span class="sig-name descname"><span class="pre">code_context</span></span><a class="headerlink" href="#inspect.FrameInfo.code_context" title="Link to this definition">¶</a></dt>
  1541. <dd><p>A list of lines of context from the source code that’s being executed by the frame
  1542. this record corresponds to.</p>
  1543. </dd></dl>
  1544. <dl class="py attribute">
  1545. <dt class="sig sig-object py" id="inspect.FrameInfo.index">
  1546. <span class="sig-name descname"><span class="pre">index</span></span><a class="headerlink" href="#inspect.FrameInfo.index" title="Link to this definition">¶</a></dt>
  1547. <dd><p>The index of the current line being executed in the <a class="reference internal" href="#inspect.FrameInfo.code_context" title="inspect.FrameInfo.code_context"><code class="xref py py-attr docutils literal notranslate"><span class="pre">code_context</span></code></a> list.</p>
  1548. </dd></dl>
  1549. <dl class="py attribute">
  1550. <dt class="sig sig-object py" id="inspect.FrameInfo.positions">
  1551. <span class="sig-name descname"><span class="pre">positions</span></span><a class="headerlink" href="#inspect.FrameInfo.positions" title="Link to this definition">¶</a></dt>
  1552. <dd><p>A <a class="reference internal" href="dis.html#dis.Positions" title="dis.Positions"><code class="xref py py-class docutils literal notranslate"><span class="pre">dis.Positions</span></code></a> object containing the start line number, end line
  1553. number, start column offset, and end column offset associated with the
  1554. instruction being executed by the frame this record corresponds to.</p>
  1555. </dd></dl>
  1556. <div class="versionchanged">
  1557. <p><span class="versionmodified changed">Changed in version 3.5: </span>Return a <a class="reference internal" href="../glossary.html#term-named-tuple"><span class="xref std std-term">named tuple</span></a> instead of a <a class="reference internal" href="stdtypes.html#tuple" title="tuple"><code class="xref py py-class docutils literal notranslate"><span class="pre">tuple</span></code></a>.</p>
  1558. </div>
  1559. <div class="versionchanged">
  1560. <p><span class="versionmodified changed">Changed in version 3.11: </span><code class="xref py py-class docutils literal notranslate"><span class="pre">FrameInfo</span></code> is now a class instance
  1561. (that is backwards compatible with the previous <a class="reference internal" href="../glossary.html#term-named-tuple"><span class="xref std std-term">named tuple</span></a>).</p>
  1562. </div>
  1563. </dd></dl>
  1564. <dl class="py class">
  1565. <dt class="sig sig-object py" id="inspect.Traceback">
  1566. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">Traceback</span></span><a class="headerlink" href="#inspect.Traceback" title="Link to this definition">¶</a></dt>
  1567. <dd><dl class="py attribute">
  1568. <dt class="sig sig-object py" id="inspect.Traceback.filename">
  1569. <span class="sig-name descname"><span class="pre">filename</span></span><a class="headerlink" href="#inspect.Traceback.filename" title="Link to this definition">¶</a></dt>
  1570. <dd><p>The file name associated with the code being executed by the frame this traceback
  1571. corresponds to.</p>
  1572. </dd></dl>
  1573. <dl class="py attribute">
  1574. <dt class="sig sig-object py" id="inspect.Traceback.lineno">
  1575. <span class="sig-name descname"><span class="pre">lineno</span></span><a class="headerlink" href="#inspect.Traceback.lineno" title="Link to this definition">¶</a></dt>
  1576. <dd><p>The line number of the current line associated with the code being
  1577. executed by the frame this traceback corresponds to.</p>
  1578. </dd></dl>
  1579. <dl class="py attribute">
  1580. <dt class="sig sig-object py" id="inspect.Traceback.function">
  1581. <span class="sig-name descname"><span class="pre">function</span></span><a class="headerlink" href="#inspect.Traceback.function" title="Link to this definition">¶</a></dt>
  1582. <dd><p>The function name that is being executed by the frame this traceback corresponds to.</p>
  1583. </dd></dl>
  1584. <dl class="py attribute">
  1585. <dt class="sig sig-object py" id="inspect.Traceback.code_context">
  1586. <span class="sig-name descname"><span class="pre">code_context</span></span><a class="headerlink" href="#inspect.Traceback.code_context" title="Link to this definition">¶</a></dt>
  1587. <dd><p>A list of lines of context from the source code that’s being executed by the frame
  1588. this traceback corresponds to.</p>
  1589. </dd></dl>
  1590. <dl class="py attribute">
  1591. <dt class="sig sig-object py" id="inspect.Traceback.index">
  1592. <span class="sig-name descname"><span class="pre">index</span></span><a class="headerlink" href="#inspect.Traceback.index" title="Link to this definition">¶</a></dt>
  1593. <dd><p>The index of the current line being executed in the <a class="reference internal" href="#inspect.Traceback.code_context" title="inspect.Traceback.code_context"><code class="xref py py-attr docutils literal notranslate"><span class="pre">code_context</span></code></a> list.</p>
  1594. </dd></dl>
  1595. <dl class="py attribute">
  1596. <dt class="sig sig-object py" id="inspect.Traceback.positions">
  1597. <span class="sig-name descname"><span class="pre">positions</span></span><a class="headerlink" href="#inspect.Traceback.positions" title="Link to this definition">¶</a></dt>
  1598. <dd><p>A <a class="reference internal" href="dis.html#dis.Positions" title="dis.Positions"><code class="xref py py-class docutils literal notranslate"><span class="pre">dis.Positions</span></code></a> object containing the start line number, end
  1599. line number, start column offset, and end column offset associated with
  1600. the instruction being executed by the frame this traceback corresponds
  1601. to.</p>
  1602. </dd></dl>
  1603. <div class="versionchanged">
  1604. <p><span class="versionmodified changed">Changed in version 3.11: </span><code class="xref py py-class docutils literal notranslate"><span class="pre">Traceback</span></code> is now a class instance
  1605. (that is backwards compatible with the previous <a class="reference internal" href="../glossary.html#term-named-tuple"><span class="xref std std-term">named tuple</span></a>).</p>
  1606. </div>
  1607. </dd></dl>
  1608. <div class="admonition note">
  1609. <p class="admonition-title">Note</p>
  1610. <p>Keeping references to frame objects, as found in the first element of the frame
  1611. records these functions return, can cause your program to create reference
  1612. cycles. Once a reference cycle has been created, the lifespan of all objects
  1613. which can be accessed from the objects which form the cycle can become much
  1614. longer even if Python’s optional cycle detector is enabled. If such cycles must
  1615. be created, it is important to ensure they are explicitly broken to avoid the
  1616. delayed destruction of objects and increased memory consumption which occurs.</p>
  1617. <p>Though the cycle detector will catch these, destruction of the frames (and local
  1618. variables) can be made deterministic by removing the cycle in a
  1619. <a class="reference internal" href="../reference/compound_stmts.html#finally"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">finally</span></code></a> clause. This is also important if the cycle detector was
  1620. disabled when Python was compiled or using <a class="reference internal" href="gc.html#gc.disable" title="gc.disable"><code class="xref py py-func docutils literal notranslate"><span class="pre">gc.disable()</span></code></a>. For example:</p>
  1621. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">handle_stackframe_without_leak</span><span class="p">():</span>
  1622. <span class="n">frame</span> <span class="o">=</span> <span class="n">inspect</span><span class="o">.</span><span class="n">currentframe</span><span class="p">()</span>
  1623. <span class="k">try</span><span class="p">:</span>
  1624. <span class="c1"># do something with the frame</span>
  1625. <span class="k">finally</span><span class="p">:</span>
  1626. <span class="k">del</span> <span class="n">frame</span>
  1627. </pre></div>
  1628. </div>
  1629. <p>If you want to keep the frame around (for example to print a traceback
  1630. later), you can also break reference cycles by using the
  1631. <a class="reference internal" href="../reference/datamodel.html#frame.clear" title="frame.clear"><code class="xref py py-meth docutils literal notranslate"><span class="pre">frame.clear()</span></code></a> method.</p>
  1632. </div>
  1633. <p>The optional <em>context</em> argument supported by most of these functions specifies
  1634. the number of lines of context to return, which are centered around the current
  1635. line.</p>
  1636. <dl class="py function">
  1637. <dt class="sig sig-object py" id="inspect.getframeinfo">
  1638. <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">getframeinfo</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">frame</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">context</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.getframeinfo" title="Link to this definition">¶</a></dt>
  1639. <dd><p>Get information about a frame or traceback object. A <a class="reference internal" href="#inspect.Traceback" title="inspect.Traceback"><code class="xref py py-class docutils literal notranslate"><span class="pre">Traceback</span></code></a> object
  1640. is returned.</p>
  1641. <div class="versionchanged">
  1642. <p><span class="versionmodified changed">Changed in version 3.11: </span>A <a class="reference internal" href="#inspect.Traceback" title="inspect.Traceback"><code class="xref py py-class docutils literal notranslate"><span class="pre">Traceback</span></code></a> object is returned instead of a named tuple.</p>
  1643. </div>
  1644. </dd></dl>
  1645. <dl class="py function">
  1646. <dt class="sig sig-object py" id="inspect.getouterframes">
  1647. <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">getouterframes</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">frame</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">context</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.getouterframes" title="Link to this definition">¶</a></dt>
  1648. <dd><p>Get a list of <a class="reference internal" href="#inspect.FrameInfo" title="inspect.FrameInfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">FrameInfo</span></code></a> objects for a frame and all outer frames.
  1649. These frames represent the calls that lead to the creation of <em>frame</em>. The
  1650. first entry in the returned list represents <em>frame</em>; the last entry
  1651. represents the outermost call on <em>frame</em>’s stack.</p>
  1652. <div class="versionchanged">
  1653. <p><span class="versionmodified changed">Changed in version 3.5: </span>A list of <a class="reference internal" href="../glossary.html#term-named-tuple"><span class="xref std std-term">named tuples</span></a>
  1654. <code class="docutils literal notranslate"><span class="pre">FrameInfo(frame,</span> <span class="pre">filename,</span> <span class="pre">lineno,</span> <span class="pre">function,</span> <span class="pre">code_context,</span> <span class="pre">index)</span></code>
  1655. is returned.</p>
  1656. </div>
  1657. <div class="versionchanged">
  1658. <p><span class="versionmodified changed">Changed in version 3.11: </span>A list of <a class="reference internal" href="#inspect.FrameInfo" title="inspect.FrameInfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">FrameInfo</span></code></a> objects is returned.</p>
  1659. </div>
  1660. </dd></dl>
  1661. <dl class="py function">
  1662. <dt class="sig sig-object py" id="inspect.getinnerframes">
  1663. <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">getinnerframes</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">traceback</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">context</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.getinnerframes" title="Link to this definition">¶</a></dt>
  1664. <dd><p>Get a list of <a class="reference internal" href="#inspect.FrameInfo" title="inspect.FrameInfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">FrameInfo</span></code></a> objects for a traceback’s frame and all
  1665. inner frames. These frames represent calls made as a consequence of <em>frame</em>.
  1666. The first entry in the list represents <em>traceback</em>; the last entry represents
  1667. where the exception was raised.</p>
  1668. <div class="versionchanged">
  1669. <p><span class="versionmodified changed">Changed in version 3.5: </span>A list of <a class="reference internal" href="../glossary.html#term-named-tuple"><span class="xref std std-term">named tuples</span></a>
  1670. <code class="docutils literal notranslate"><span class="pre">FrameInfo(frame,</span> <span class="pre">filename,</span> <span class="pre">lineno,</span> <span class="pre">function,</span> <span class="pre">code_context,</span> <span class="pre">index)</span></code>
  1671. is returned.</p>
  1672. </div>
  1673. <div class="versionchanged">
  1674. <p><span class="versionmodified changed">Changed in version 3.11: </span>A list of <a class="reference internal" href="#inspect.FrameInfo" title="inspect.FrameInfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">FrameInfo</span></code></a> objects is returned.</p>
  1675. </div>
  1676. </dd></dl>
  1677. <dl class="py function">
  1678. <dt class="sig sig-object py" id="inspect.currentframe">
  1679. <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">currentframe</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#inspect.currentframe" title="Link to this definition">¶</a></dt>
  1680. <dd><p>Return the frame object for the caller’s stack frame.</p>
  1681. <div class="impl-detail compound">
  1682. <p><strong>CPython implementation detail:</strong> This function relies on Python stack frame support in the interpreter,
  1683. which isn’t guaranteed to exist in all implementations of Python. If
  1684. running in an implementation without Python stack frame support this
  1685. function returns <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  1686. </div>
  1687. </dd></dl>
  1688. <dl class="py function">
  1689. <dt class="sig sig-object py" id="inspect.stack">
  1690. <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">stack</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">context</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.stack" title="Link to this definition">¶</a></dt>
  1691. <dd><p>Return a list of <a class="reference internal" href="#inspect.FrameInfo" title="inspect.FrameInfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">FrameInfo</span></code></a> objects for the caller’s stack. The
  1692. first entry in the returned list represents the caller; the last entry
  1693. represents the outermost call on the stack.</p>
  1694. <div class="versionchanged">
  1695. <p><span class="versionmodified changed">Changed in version 3.5: </span>A list of <a class="reference internal" href="../glossary.html#term-named-tuple"><span class="xref std std-term">named tuples</span></a>
  1696. <code class="docutils literal notranslate"><span class="pre">FrameInfo(frame,</span> <span class="pre">filename,</span> <span class="pre">lineno,</span> <span class="pre">function,</span> <span class="pre">code_context,</span> <span class="pre">index)</span></code>
  1697. is returned.</p>
  1698. </div>
  1699. <div class="versionchanged">
  1700. <p><span class="versionmodified changed">Changed in version 3.11: </span>A list of <a class="reference internal" href="#inspect.FrameInfo" title="inspect.FrameInfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">FrameInfo</span></code></a> objects is returned.</p>
  1701. </div>
  1702. </dd></dl>
  1703. <dl class="py function">
  1704. <dt class="sig sig-object py" id="inspect.trace">
  1705. <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">trace</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">context</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.trace" title="Link to this definition">¶</a></dt>
  1706. <dd><p>Return a list of <a class="reference internal" href="#inspect.FrameInfo" title="inspect.FrameInfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">FrameInfo</span></code></a> objects for the stack between the current
  1707. frame and the frame in which an exception currently being handled was raised
  1708. in. The first entry in the list represents the caller; the last entry
  1709. represents where the exception was raised.</p>
  1710. <div class="versionchanged">
  1711. <p><span class="versionmodified changed">Changed in version 3.5: </span>A list of <a class="reference internal" href="../glossary.html#term-named-tuple"><span class="xref std std-term">named tuples</span></a>
  1712. <code class="docutils literal notranslate"><span class="pre">FrameInfo(frame,</span> <span class="pre">filename,</span> <span class="pre">lineno,</span> <span class="pre">function,</span> <span class="pre">code_context,</span> <span class="pre">index)</span></code>
  1713. is returned.</p>
  1714. </div>
  1715. <div class="versionchanged">
  1716. <p><span class="versionmodified changed">Changed in version 3.11: </span>A list of <a class="reference internal" href="#inspect.FrameInfo" title="inspect.FrameInfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">FrameInfo</span></code></a> objects is returned.</p>
  1717. </div>
  1718. </dd></dl>
  1719. </section>
  1720. <section id="fetching-attributes-statically">
  1721. <h2>Fetching attributes statically<a class="headerlink" href="#fetching-attributes-statically" title="Link to this heading">¶</a></h2>
  1722. <p>Both <a class="reference internal" href="functions.html#getattr" title="getattr"><code class="xref py py-func docutils literal notranslate"><span class="pre">getattr()</span></code></a> and <a class="reference internal" href="functions.html#hasattr" title="hasattr"><code class="xref py py-func docutils literal notranslate"><span class="pre">hasattr()</span></code></a> can trigger code execution when
  1723. fetching or checking for the existence of attributes. Descriptors, like
  1724. properties, will be invoked and <a class="reference internal" href="../reference/datamodel.html#object.__getattr__" title="object.__getattr__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__getattr__()</span></code></a> and
  1725. <a class="reference internal" href="../reference/datamodel.html#object.__getattribute__" title="object.__getattribute__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__getattribute__()</span></code></a>
  1726. may be called.</p>
  1727. <p>For cases where you want passive introspection, like documentation tools, this
  1728. can be inconvenient. <a class="reference internal" href="#inspect.getattr_static" title="inspect.getattr_static"><code class="xref py py-func docutils literal notranslate"><span class="pre">getattr_static()</span></code></a> has the same signature as <a class="reference internal" href="functions.html#getattr" title="getattr"><code class="xref py py-func docutils literal notranslate"><span class="pre">getattr()</span></code></a>
  1729. but avoids executing code when it fetches attributes.</p>
  1730. <dl class="py function">
  1731. <dt class="sig sig-object py" id="inspect.getattr_static">
  1732. <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">getattr_static</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">obj</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">attr</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">default</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.getattr_static" title="Link to this definition">¶</a></dt>
  1733. <dd><p>Retrieve attributes without triggering dynamic lookup via the
  1734. descriptor protocol, <a class="reference internal" href="../reference/datamodel.html#object.__getattr__" title="object.__getattr__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__getattr__()</span></code></a>
  1735. or <a class="reference internal" href="../reference/datamodel.html#object.__getattribute__" title="object.__getattribute__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__getattribute__()</span></code></a>.</p>
  1736. <p>Note: this function may not be able to retrieve all attributes
  1737. that getattr can fetch (like dynamically created attributes)
  1738. and may find attributes that getattr can’t (like descriptors
  1739. that raise AttributeError). It can also return descriptors objects
  1740. instead of instance members.</p>
  1741. <p>If the instance <a class="reference internal" href="stdtypes.html#object.__dict__" title="object.__dict__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__dict__</span></code></a> is shadowed by another member (for
  1742. example a property) then this function will be unable to find instance
  1743. members.</p>
  1744. <div class="versionadded">
  1745. <p><span class="versionmodified added">New in version 3.2.</span></p>
  1746. </div>
  1747. </dd></dl>
  1748. <p><a class="reference internal" href="#inspect.getattr_static" title="inspect.getattr_static"><code class="xref py py-func docutils literal notranslate"><span class="pre">getattr_static()</span></code></a> does not resolve descriptors, for example slot descriptors or
  1749. getset descriptors on objects implemented in C. The descriptor object
  1750. is returned instead of the underlying attribute.</p>
  1751. <p>You can handle these with code like the following. Note that
  1752. for arbitrary getset descriptors invoking these may trigger
  1753. code execution:</p>
  1754. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># example code for resolving the builtin descriptor types</span>
  1755. <span class="k">class</span> <span class="nc">_foo</span><span class="p">:</span>
  1756. <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;foo&#39;</span><span class="p">]</span>
  1757. <span class="n">slot_descriptor</span> <span class="o">=</span> <span class="nb">type</span><span class="p">(</span><span class="n">_foo</span><span class="o">.</span><span class="n">foo</span><span class="p">)</span>
  1758. <span class="n">getset_descriptor</span> <span class="o">=</span> <span class="nb">type</span><span class="p">(</span><span class="nb">type</span><span class="p">(</span><span class="nb">open</span><span class="p">(</span><span class="vm">__file__</span><span class="p">))</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
  1759. <span class="n">wrapper_descriptor</span> <span class="o">=</span> <span class="nb">type</span><span class="p">(</span><span class="nb">str</span><span class="o">.</span><span class="vm">__dict__</span><span class="p">[</span><span class="s1">&#39;__add__&#39;</span><span class="p">])</span>
  1760. <span class="n">descriptor_types</span> <span class="o">=</span> <span class="p">(</span><span class="n">slot_descriptor</span><span class="p">,</span> <span class="n">getset_descriptor</span><span class="p">,</span> <span class="n">wrapper_descriptor</span><span class="p">)</span>
  1761. <span class="n">result</span> <span class="o">=</span> <span class="n">getattr_static</span><span class="p">(</span><span class="n">some_object</span><span class="p">,</span> <span class="s1">&#39;foo&#39;</span><span class="p">)</span>
  1762. <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">result</span><span class="p">)</span> <span class="ow">in</span> <span class="n">descriptor_types</span><span class="p">:</span>
  1763. <span class="k">try</span><span class="p">:</span>
  1764. <span class="n">result</span> <span class="o">=</span> <span class="n">result</span><span class="o">.</span><span class="fm">__get__</span><span class="p">()</span>
  1765. <span class="k">except</span> <span class="ne">AttributeError</span><span class="p">:</span>
  1766. <span class="c1"># descriptors can raise AttributeError to</span>
  1767. <span class="c1"># indicate there is no underlying value</span>
  1768. <span class="c1"># in which case the descriptor itself will</span>
  1769. <span class="c1"># have to do</span>
  1770. <span class="k">pass</span>
  1771. </pre></div>
  1772. </div>
  1773. </section>
  1774. <section id="current-state-of-generators-coroutines-and-asynchronous-generators">
  1775. <h2>Current State of Generators, Coroutines, and Asynchronous Generators<a class="headerlink" href="#current-state-of-generators-coroutines-and-asynchronous-generators" title="Link to this heading">¶</a></h2>
  1776. <p>When implementing coroutine schedulers and for other advanced uses of
  1777. generators, it is useful to determine whether a generator is currently
  1778. executing, is waiting to start or resume or execution, or has already
  1779. terminated. <a class="reference internal" href="#inspect.getgeneratorstate" title="inspect.getgeneratorstate"><code class="xref py py-func docutils literal notranslate"><span class="pre">getgeneratorstate()</span></code></a> allows the current state of a
  1780. generator to be determined easily.</p>
  1781. <dl class="py function">
  1782. <dt class="sig sig-object py" id="inspect.getgeneratorstate">
  1783. <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">getgeneratorstate</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">generator</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.getgeneratorstate" title="Link to this definition">¶</a></dt>
  1784. <dd><p>Get current state of a generator-iterator.</p>
  1785. <p>Possible states are:</p>
  1786. <ul class="simple">
  1787. <li><p>GEN_CREATED: Waiting to start execution.</p></li>
  1788. <li><p>GEN_RUNNING: Currently being executed by the interpreter.</p></li>
  1789. <li><p>GEN_SUSPENDED: Currently suspended at a yield expression.</p></li>
  1790. <li><p>GEN_CLOSED: Execution has completed.</p></li>
  1791. </ul>
  1792. <div class="versionadded">
  1793. <p><span class="versionmodified added">New in version 3.2.</span></p>
  1794. </div>
  1795. </dd></dl>
  1796. <dl class="py function">
  1797. <dt class="sig sig-object py" id="inspect.getcoroutinestate">
  1798. <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">getcoroutinestate</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">coroutine</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.getcoroutinestate" title="Link to this definition">¶</a></dt>
  1799. <dd><p>Get current state of a coroutine object. The function is intended to be
  1800. used with coroutine objects created by <a class="reference internal" href="../reference/compound_stmts.html#async-def"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">async</span> <span class="pre">def</span></code></a> functions, but
  1801. will accept any coroutine-like object that has <code class="docutils literal notranslate"><span class="pre">cr_running</span></code> and
  1802. <code class="docutils literal notranslate"><span class="pre">cr_frame</span></code> attributes.</p>
  1803. <p>Possible states are:</p>
  1804. <ul class="simple">
  1805. <li><p>CORO_CREATED: Waiting to start execution.</p></li>
  1806. <li><p>CORO_RUNNING: Currently being executed by the interpreter.</p></li>
  1807. <li><p>CORO_SUSPENDED: Currently suspended at an await expression.</p></li>
  1808. <li><p>CORO_CLOSED: Execution has completed.</p></li>
  1809. </ul>
  1810. <div class="versionadded">
  1811. <p><span class="versionmodified added">New in version 3.5.</span></p>
  1812. </div>
  1813. </dd></dl>
  1814. <dl class="py function">
  1815. <dt class="sig sig-object py" id="inspect.getasyncgenstate">
  1816. <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">getasyncgenstate</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">agen</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.getasyncgenstate" title="Link to this definition">¶</a></dt>
  1817. <dd><p>Get current state of an asynchronous generator object. The function is
  1818. intended to be used with asynchronous iterator objects created by
  1819. <a class="reference internal" href="../reference/compound_stmts.html#async-def"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">async</span> <span class="pre">def</span></code></a> functions which use the <a class="reference internal" href="../reference/simple_stmts.html#yield"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">yield</span></code></a> statement,
  1820. but will accept any asynchronous generator-like object that has
  1821. <code class="docutils literal notranslate"><span class="pre">ag_running</span></code> and <code class="docutils literal notranslate"><span class="pre">ag_frame</span></code> attributes.</p>
  1822. <p>Possible states are:</p>
  1823. <ul class="simple">
  1824. <li><p>AGEN_CREATED: Waiting to start execution.</p></li>
  1825. <li><p>AGEN_RUNNING: Currently being executed by the interpreter.</p></li>
  1826. <li><p>AGEN_SUSPENDED: Currently suspended at a yield expression.</p></li>
  1827. <li><p>AGEN_CLOSED: Execution has completed.</p></li>
  1828. </ul>
  1829. <div class="versionadded">
  1830. <p><span class="versionmodified added">New in version 3.12.</span></p>
  1831. </div>
  1832. </dd></dl>
  1833. <p>The current internal state of the generator can also be queried. This is
  1834. mostly useful for testing purposes, to ensure that internal state is being
  1835. updated as expected:</p>
  1836. <dl class="py function">
  1837. <dt class="sig sig-object py" id="inspect.getgeneratorlocals">
  1838. <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">getgeneratorlocals</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">generator</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.getgeneratorlocals" title="Link to this definition">¶</a></dt>
  1839. <dd><p>Get the mapping of live local variables in <em>generator</em> to their current
  1840. values. A dictionary is returned that maps from variable names to values.
  1841. This is the equivalent of calling <a class="reference internal" href="functions.html#locals" title="locals"><code class="xref py py-func docutils literal notranslate"><span class="pre">locals()</span></code></a> in the body of the
  1842. generator, and all the same caveats apply.</p>
  1843. <p>If <em>generator</em> is a <a class="reference internal" href="../glossary.html#term-generator"><span class="xref std std-term">generator</span></a> with no currently associated frame,
  1844. then an empty dictionary is returned. <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> is raised if
  1845. <em>generator</em> is not a Python generator object.</p>
  1846. <div class="impl-detail compound">
  1847. <p><strong>CPython implementation detail:</strong> This function relies on the generator exposing a Python stack frame
  1848. for introspection, which isn’t guaranteed to be the case in all
  1849. implementations of Python. In such cases, this function will always
  1850. return an empty dictionary.</p>
  1851. </div>
  1852. <div class="versionadded">
  1853. <p><span class="versionmodified added">New in version 3.3.</span></p>
  1854. </div>
  1855. </dd></dl>
  1856. <dl class="py function">
  1857. <dt class="sig sig-object py" id="inspect.getcoroutinelocals">
  1858. <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">getcoroutinelocals</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">coroutine</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.getcoroutinelocals" title="Link to this definition">¶</a></dt>
  1859. <dd><p>This function is analogous to <a class="reference internal" href="#inspect.getgeneratorlocals" title="inspect.getgeneratorlocals"><code class="xref py py-func docutils literal notranslate"><span class="pre">getgeneratorlocals()</span></code></a>, but
  1860. works for coroutine objects created by <a class="reference internal" href="../reference/compound_stmts.html#async-def"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">async</span> <span class="pre">def</span></code></a> functions.</p>
  1861. <div class="versionadded">
  1862. <p><span class="versionmodified added">New in version 3.5.</span></p>
  1863. </div>
  1864. </dd></dl>
  1865. <dl class="py function">
  1866. <dt class="sig sig-object py" id="inspect.getasyncgenlocals">
  1867. <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">getasyncgenlocals</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">agen</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.getasyncgenlocals" title="Link to this definition">¶</a></dt>
  1868. <dd><p>This function is analogous to <a class="reference internal" href="#inspect.getgeneratorlocals" title="inspect.getgeneratorlocals"><code class="xref py py-func docutils literal notranslate"><span class="pre">getgeneratorlocals()</span></code></a>, but
  1869. works for asynchronous generator objects created by <a class="reference internal" href="../reference/compound_stmts.html#async-def"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">async</span> <span class="pre">def</span></code></a>
  1870. functions which use the <a class="reference internal" href="../reference/simple_stmts.html#yield"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">yield</span></code></a> statement.</p>
  1871. <div class="versionadded">
  1872. <p><span class="versionmodified added">New in version 3.12.</span></p>
  1873. </div>
  1874. </dd></dl>
  1875. </section>
  1876. <section id="code-objects-bit-flags">
  1877. <span id="inspect-module-co-flags"></span><h2>Code Objects Bit Flags<a class="headerlink" href="#code-objects-bit-flags" title="Link to this heading">¶</a></h2>
  1878. <p>Python code objects have a <a class="reference internal" href="../reference/datamodel.html#codeobject.co_flags" title="codeobject.co_flags"><code class="xref py py-attr docutils literal notranslate"><span class="pre">co_flags</span></code></a> attribute,
  1879. which is a bitmap of the following flags:</p>
  1880. <dl class="py data">
  1881. <dt class="sig sig-object py" id="inspect.CO_OPTIMIZED">
  1882. <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">CO_OPTIMIZED</span></span><a class="headerlink" href="#inspect.CO_OPTIMIZED" title="Link to this definition">¶</a></dt>
  1883. <dd><p>The code object is optimized, using fast locals.</p>
  1884. </dd></dl>
  1885. <dl class="py data">
  1886. <dt class="sig sig-object py" id="inspect.CO_NEWLOCALS">
  1887. <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">CO_NEWLOCALS</span></span><a class="headerlink" href="#inspect.CO_NEWLOCALS" title="Link to this definition">¶</a></dt>
  1888. <dd><p>If set, a new dict will be created for the frame’s <a class="reference internal" href="../reference/datamodel.html#frame.f_locals" title="frame.f_locals"><code class="xref py py-attr docutils literal notranslate"><span class="pre">f_locals</span></code></a>
  1889. when the code object is executed.</p>
  1890. </dd></dl>
  1891. <dl class="py data">
  1892. <dt class="sig sig-object py" id="inspect.CO_VARARGS">
  1893. <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">CO_VARARGS</span></span><a class="headerlink" href="#inspect.CO_VARARGS" title="Link to this definition">¶</a></dt>
  1894. <dd><p>The code object has a variable positional parameter (<code class="docutils literal notranslate"><span class="pre">*args</span></code>-like).</p>
  1895. </dd></dl>
  1896. <dl class="py data">
  1897. <dt class="sig sig-object py" id="inspect.CO_VARKEYWORDS">
  1898. <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">CO_VARKEYWORDS</span></span><a class="headerlink" href="#inspect.CO_VARKEYWORDS" title="Link to this definition">¶</a></dt>
  1899. <dd><p>The code object has a variable keyword parameter (<code class="docutils literal notranslate"><span class="pre">**kwargs</span></code>-like).</p>
  1900. </dd></dl>
  1901. <dl class="py data">
  1902. <dt class="sig sig-object py" id="inspect.CO_NESTED">
  1903. <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">CO_NESTED</span></span><a class="headerlink" href="#inspect.CO_NESTED" title="Link to this definition">¶</a></dt>
  1904. <dd><p>The flag is set when the code object is a nested function.</p>
  1905. </dd></dl>
  1906. <dl class="py data">
  1907. <dt class="sig sig-object py" id="inspect.CO_GENERATOR">
  1908. <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">CO_GENERATOR</span></span><a class="headerlink" href="#inspect.CO_GENERATOR" title="Link to this definition">¶</a></dt>
  1909. <dd><p>The flag is set when the code object is a generator function, i.e.
  1910. a generator object is returned when the code object is executed.</p>
  1911. </dd></dl>
  1912. <dl class="py data">
  1913. <dt class="sig sig-object py" id="inspect.CO_COROUTINE">
  1914. <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">CO_COROUTINE</span></span><a class="headerlink" href="#inspect.CO_COROUTINE" title="Link to this definition">¶</a></dt>
  1915. <dd><p>The flag is set when the code object is a coroutine function.
  1916. When the code object is executed it returns a coroutine object.
  1917. See <span class="target" id="index-1"></span><a class="pep reference external" href="https://peps.python.org/pep-0492/"><strong>PEP 492</strong></a> for more details.</p>
  1918. <div class="versionadded">
  1919. <p><span class="versionmodified added">New in version 3.5.</span></p>
  1920. </div>
  1921. </dd></dl>
  1922. <dl class="py data">
  1923. <dt class="sig sig-object py" id="inspect.CO_ITERABLE_COROUTINE">
  1924. <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">CO_ITERABLE_COROUTINE</span></span><a class="headerlink" href="#inspect.CO_ITERABLE_COROUTINE" title="Link to this definition">¶</a></dt>
  1925. <dd><p>The flag is used to transform generators into generator-based
  1926. coroutines. Generator objects with this flag can be used in
  1927. <code class="docutils literal notranslate"><span class="pre">await</span></code> expression, and can <code class="docutils literal notranslate"><span class="pre">yield</span> <span class="pre">from</span></code> coroutine objects.
  1928. See <span class="target" id="index-2"></span><a class="pep reference external" href="https://peps.python.org/pep-0492/"><strong>PEP 492</strong></a> for more details.</p>
  1929. <div class="versionadded">
  1930. <p><span class="versionmodified added">New in version 3.5.</span></p>
  1931. </div>
  1932. </dd></dl>
  1933. <dl class="py data">
  1934. <dt class="sig sig-object py" id="inspect.CO_ASYNC_GENERATOR">
  1935. <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">CO_ASYNC_GENERATOR</span></span><a class="headerlink" href="#inspect.CO_ASYNC_GENERATOR" title="Link to this definition">¶</a></dt>
  1936. <dd><p>The flag is set when the code object is an asynchronous generator
  1937. function. When the code object is executed it returns an
  1938. asynchronous generator object. See <span class="target" id="index-3"></span><a class="pep reference external" href="https://peps.python.org/pep-0525/"><strong>PEP 525</strong></a> for more details.</p>
  1939. <div class="versionadded">
  1940. <p><span class="versionmodified added">New in version 3.6.</span></p>
  1941. </div>
  1942. </dd></dl>
  1943. <div class="admonition note">
  1944. <p class="admonition-title">Note</p>
  1945. <p>The flags are specific to CPython, and may not be defined in other
  1946. Python implementations. Furthermore, the flags are an implementation
  1947. detail, and can be removed or deprecated in future Python releases.
  1948. It’s recommended to use public APIs from the <a class="reference internal" href="#module-inspect" title="inspect: Extract information and source code from live objects."><code class="xref py py-mod docutils literal notranslate"><span class="pre">inspect</span></code></a> module
  1949. for any introspection needs.</p>
  1950. </div>
  1951. </section>
  1952. <section id="buffer-flags">
  1953. <h2>Buffer flags<a class="headerlink" href="#buffer-flags" title="Link to this heading">¶</a></h2>
  1954. <dl class="py class">
  1955. <dt class="sig sig-object py" id="inspect.BufferFlags">
  1956. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">BufferFlags</span></span><a class="headerlink" href="#inspect.BufferFlags" title="Link to this definition">¶</a></dt>
  1957. <dd><p>This is an <a class="reference internal" href="enum.html#enum.IntFlag" title="enum.IntFlag"><code class="xref py py-class docutils literal notranslate"><span class="pre">enum.IntFlag</span></code></a> that represents the flags that
  1958. can be passed to the <a class="reference internal" href="../reference/datamodel.html#object.__buffer__" title="object.__buffer__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__buffer__()</span></code></a> method of objects
  1959. implementing the <a class="reference internal" href="../c-api/buffer.html#bufferobjects"><span class="std std-ref">buffer protocol</span></a>.</p>
  1960. <p>The meaning of the flags is explained at <a class="reference internal" href="../c-api/buffer.html#buffer-request-types"><span class="std std-ref">Buffer request types</span></a>.</p>
  1961. <dl class="py attribute">
  1962. <dt class="sig sig-object py" id="inspect.BufferFlags.SIMPLE">
  1963. <span class="sig-name descname"><span class="pre">SIMPLE</span></span><a class="headerlink" href="#inspect.BufferFlags.SIMPLE" title="Link to this definition">¶</a></dt>
  1964. <dd></dd></dl>
  1965. <dl class="py attribute">
  1966. <dt class="sig sig-object py" id="inspect.BufferFlags.WRITABLE">
  1967. <span class="sig-name descname"><span class="pre">WRITABLE</span></span><a class="headerlink" href="#inspect.BufferFlags.WRITABLE" title="Link to this definition">¶</a></dt>
  1968. <dd></dd></dl>
  1969. <dl class="py attribute">
  1970. <dt class="sig sig-object py" id="inspect.BufferFlags.FORMAT">
  1971. <span class="sig-name descname"><span class="pre">FORMAT</span></span><a class="headerlink" href="#inspect.BufferFlags.FORMAT" title="Link to this definition">¶</a></dt>
  1972. <dd></dd></dl>
  1973. <dl class="py attribute">
  1974. <dt class="sig sig-object py" id="inspect.BufferFlags.ND">
  1975. <span class="sig-name descname"><span class="pre">ND</span></span><a class="headerlink" href="#inspect.BufferFlags.ND" title="Link to this definition">¶</a></dt>
  1976. <dd></dd></dl>
  1977. <dl class="py attribute">
  1978. <dt class="sig sig-object py" id="inspect.BufferFlags.STRIDES">
  1979. <span class="sig-name descname"><span class="pre">STRIDES</span></span><a class="headerlink" href="#inspect.BufferFlags.STRIDES" title="Link to this definition">¶</a></dt>
  1980. <dd></dd></dl>
  1981. <dl class="py attribute">
  1982. <dt class="sig sig-object py" id="inspect.BufferFlags.C_CONTIGUOUS">
  1983. <span class="sig-name descname"><span class="pre">C_CONTIGUOUS</span></span><a class="headerlink" href="#inspect.BufferFlags.C_CONTIGUOUS" title="Link to this definition">¶</a></dt>
  1984. <dd></dd></dl>
  1985. <dl class="py attribute">
  1986. <dt class="sig sig-object py" id="inspect.BufferFlags.F_CONTIGUOUS">
  1987. <span class="sig-name descname"><span class="pre">F_CONTIGUOUS</span></span><a class="headerlink" href="#inspect.BufferFlags.F_CONTIGUOUS" title="Link to this definition">¶</a></dt>
  1988. <dd></dd></dl>
  1989. <dl class="py attribute">
  1990. <dt class="sig sig-object py" id="inspect.BufferFlags.ANY_CONTIGUOUS">
  1991. <span class="sig-name descname"><span class="pre">ANY_CONTIGUOUS</span></span><a class="headerlink" href="#inspect.BufferFlags.ANY_CONTIGUOUS" title="Link to this definition">¶</a></dt>
  1992. <dd></dd></dl>
  1993. <dl class="py attribute">
  1994. <dt class="sig sig-object py" id="inspect.BufferFlags.INDIRECT">
  1995. <span class="sig-name descname"><span class="pre">INDIRECT</span></span><a class="headerlink" href="#inspect.BufferFlags.INDIRECT" title="Link to this definition">¶</a></dt>
  1996. <dd></dd></dl>
  1997. <dl class="py attribute">
  1998. <dt class="sig sig-object py" id="inspect.BufferFlags.CONTIG">
  1999. <span class="sig-name descname"><span class="pre">CONTIG</span></span><a class="headerlink" href="#inspect.BufferFlags.CONTIG" title="Link to this definition">¶</a></dt>
  2000. <dd></dd></dl>
  2001. <dl class="py attribute">
  2002. <dt class="sig sig-object py" id="inspect.BufferFlags.CONTIG_RO">
  2003. <span class="sig-name descname"><span class="pre">CONTIG_RO</span></span><a class="headerlink" href="#inspect.BufferFlags.CONTIG_RO" title="Link to this definition">¶</a></dt>
  2004. <dd></dd></dl>
  2005. <dl class="py attribute">
  2006. <dt class="sig sig-object py" id="inspect.BufferFlags.STRIDED">
  2007. <span class="sig-name descname"><span class="pre">STRIDED</span></span><a class="headerlink" href="#inspect.BufferFlags.STRIDED" title="Link to this definition">¶</a></dt>
  2008. <dd></dd></dl>
  2009. <dl class="py attribute">
  2010. <dt class="sig sig-object py" id="inspect.BufferFlags.STRIDED_RO">
  2011. <span class="sig-name descname"><span class="pre">STRIDED_RO</span></span><a class="headerlink" href="#inspect.BufferFlags.STRIDED_RO" title="Link to this definition">¶</a></dt>
  2012. <dd></dd></dl>
  2013. <dl class="py attribute">
  2014. <dt class="sig sig-object py" id="inspect.BufferFlags.RECORDS">
  2015. <span class="sig-name descname"><span class="pre">RECORDS</span></span><a class="headerlink" href="#inspect.BufferFlags.RECORDS" title="Link to this definition">¶</a></dt>
  2016. <dd></dd></dl>
  2017. <dl class="py attribute">
  2018. <dt class="sig sig-object py" id="inspect.BufferFlags.RECORDS_RO">
  2019. <span class="sig-name descname"><span class="pre">RECORDS_RO</span></span><a class="headerlink" href="#inspect.BufferFlags.RECORDS_RO" title="Link to this definition">¶</a></dt>
  2020. <dd></dd></dl>
  2021. <dl class="py attribute">
  2022. <dt class="sig sig-object py" id="inspect.BufferFlags.FULL">
  2023. <span class="sig-name descname"><span class="pre">FULL</span></span><a class="headerlink" href="#inspect.BufferFlags.FULL" title="Link to this definition">¶</a></dt>
  2024. <dd></dd></dl>
  2025. <dl class="py attribute">
  2026. <dt class="sig sig-object py" id="inspect.BufferFlags.FULL_RO">
  2027. <span class="sig-name descname"><span class="pre">FULL_RO</span></span><a class="headerlink" href="#inspect.BufferFlags.FULL_RO" title="Link to this definition">¶</a></dt>
  2028. <dd></dd></dl>
  2029. <dl class="py attribute">
  2030. <dt class="sig sig-object py" id="inspect.BufferFlags.READ">
  2031. <span class="sig-name descname"><span class="pre">READ</span></span><a class="headerlink" href="#inspect.BufferFlags.READ" title="Link to this definition">¶</a></dt>
  2032. <dd></dd></dl>
  2033. <dl class="py attribute">
  2034. <dt class="sig sig-object py" id="inspect.BufferFlags.WRITE">
  2035. <span class="sig-name descname"><span class="pre">WRITE</span></span><a class="headerlink" href="#inspect.BufferFlags.WRITE" title="Link to this definition">¶</a></dt>
  2036. <dd></dd></dl>
  2037. <div class="versionadded">
  2038. <p><span class="versionmodified added">New in version 3.12.</span></p>
  2039. </div>
  2040. </dd></dl>
  2041. </section>
  2042. <section id="command-line-interface">
  2043. <span id="inspect-module-cli"></span><h2>Command Line Interface<a class="headerlink" href="#command-line-interface" title="Link to this heading">¶</a></h2>
  2044. <p>The <a class="reference internal" href="#module-inspect" title="inspect: Extract information and source code from live objects."><code class="xref py py-mod docutils literal notranslate"><span class="pre">inspect</span></code></a> module also provides a basic introspection capability
  2045. from the command line.</p>
  2046. <p>By default, accepts the name of a module and prints the source of that
  2047. module. A class or function within the module can be printed instead by
  2048. appended a colon and the qualified name of the target object.</p>
  2049. <dl class="std option">
  2050. <dt class="sig sig-object std" id="cmdoption-inspect-details">
  2051. <span class="sig-name descname"><span class="pre">--details</span></span><span class="sig-prename descclassname"></span><a class="headerlink" href="#cmdoption-inspect-details" title="Link to this definition">¶</a></dt>
  2052. <dd><p>Print information about the specified object rather than the source code</p>
  2053. </dd></dl>
  2054. </section>
  2055. </section>
  2056. <div class="clearer"></div>
  2057. </div>
  2058. </div>
  2059. </div>
  2060. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  2061. <div class="sphinxsidebarwrapper">
  2062. <div>
  2063. <h3><a href="../contents.html">Table of Contents</a></h3>
  2064. <ul>
  2065. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">inspect</span></code> — Inspect live objects</a><ul>
  2066. <li><a class="reference internal" href="#types-and-members">Types and members</a></li>
  2067. <li><a class="reference internal" href="#retrieving-source-code">Retrieving source code</a></li>
  2068. <li><a class="reference internal" href="#introspecting-callables-with-the-signature-object">Introspecting callables with the Signature object</a></li>
  2069. <li><a class="reference internal" href="#classes-and-functions">Classes and functions</a></li>
  2070. <li><a class="reference internal" href="#the-interpreter-stack">The interpreter stack</a></li>
  2071. <li><a class="reference internal" href="#fetching-attributes-statically">Fetching attributes statically</a></li>
  2072. <li><a class="reference internal" href="#current-state-of-generators-coroutines-and-asynchronous-generators">Current State of Generators, Coroutines, and Asynchronous Generators</a></li>
  2073. <li><a class="reference internal" href="#code-objects-bit-flags">Code Objects Bit Flags</a></li>
  2074. <li><a class="reference internal" href="#buffer-flags">Buffer flags</a></li>
  2075. <li><a class="reference internal" href="#command-line-interface">Command Line Interface</a></li>
  2076. </ul>
  2077. </li>
  2078. </ul>
  2079. </div>
  2080. <div>
  2081. <h4>Previous topic</h4>
  2082. <p class="topless"><a href="gc.html"
  2083. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gc</span></code> — Garbage Collector interface</a></p>
  2084. </div>
  2085. <div>
  2086. <h4>Next topic</h4>
  2087. <p class="topless"><a href="site.html"
  2088. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">site</span></code> — Site-specific configuration hook</a></p>
  2089. </div>
  2090. <div role="note" aria-label="source link">
  2091. <h3>This Page</h3>
  2092. <ul class="this-page-menu">
  2093. <li><a href="../bugs.html">Report a Bug</a></li>
  2094. <li>
  2095. <a href="https://github.com/python/cpython/blob/main/Doc/library/inspect.rst"
  2096. rel="nofollow">Show Source
  2097. </a>
  2098. </li>
  2099. </ul>
  2100. </div>
  2101. </div>
  2102. <div id="sidebarbutton" title="Collapse sidebar">
  2103. <span>«</span>
  2104. </div>
  2105. </div>
  2106. <div class="clearer"></div>
  2107. </div>
  2108. <div class="related" role="navigation" aria-label="related navigation">
  2109. <h3>Navigation</h3>
  2110. <ul>
  2111. <li class="right" style="margin-right: 10px">
  2112. <a href="../genindex.html" title="General Index"
  2113. >index</a></li>
  2114. <li class="right" >
  2115. <a href="../py-modindex.html" title="Python Module Index"
  2116. >modules</a> |</li>
  2117. <li class="right" >
  2118. <a href="site.html" title="site — Site-specific configuration hook"
  2119. >next</a> |</li>
  2120. <li class="right" >
  2121. <a href="gc.html" title="gc — Garbage Collector interface"
  2122. >previous</a> |</li>
  2123. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  2124. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  2125. <li class="switchers">
  2126. <div class="language_switcher_placeholder"></div>
  2127. <div class="version_switcher_placeholder"></div>
  2128. </li>
  2129. <li>
  2130. </li>
  2131. <li id="cpython-language-and-version">
  2132. <a href="../index.html">3.12.3 Documentation</a> &#187;
  2133. </li>
  2134. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  2135. <li class="nav-item nav-item-2"><a href="python.html" >Python Runtime Services</a> &#187;</li>
  2136. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">inspect</span></code> — Inspect live objects</a></li>
  2137. <li class="right">
  2138. <div class="inline-search" role="search">
  2139. <form class="inline-search" action="../search.html" method="get">
  2140. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  2141. <input type="submit" value="Go" />
  2142. </form>
  2143. </div>
  2144. |
  2145. </li>
  2146. <li class="right">
  2147. <label class="theme-selector-label">
  2148. Theme
  2149. <select class="theme-selector" oninput="activateTheme(this.value)">
  2150. <option value="auto" selected>Auto</option>
  2151. <option value="light">Light</option>
  2152. <option value="dark">Dark</option>
  2153. </select>
  2154. </label> |</li>
  2155. </ul>
  2156. </div>
  2157. <div class="footer">
  2158. &copy;
  2159. <a href="../copyright.html">
  2160. Copyright
  2161. </a>
  2162. 2001-2024, Python Software Foundation.
  2163. <br />
  2164. This page is licensed under the Python Software Foundation License Version 2.
  2165. <br />
  2166. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  2167. <br />
  2168. See <a href="/license.html">History and License</a> for more information.<br />
  2169. <br />
  2170. The Python Software Foundation is a non-profit corporation.
  2171. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  2172. <br />
  2173. <br />
  2174. Last updated on Apr 09, 2024 (13:47 UTC).
  2175. <a href="/bugs.html">Found a bug</a>?
  2176. <br />
  2177. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  2178. </div>
  2179. </body>
  2180. </html>
上海开阖软件有限公司 沪ICP备12045867号-1