parthtamu/rag-code-assistant
0
1<!DOCTYPE html>2 3<html lang="en" data-content_root="../">4 <head>5 <meta charset="utf-8" />6 <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />7<meta property="og:title" content="inspect — Inspect live objects" />8<meta property="og:type" content="website" />9<meta property="og:url" content="https://docs.python.org/3/library/inspect.html" />10<meta property="og:site_name" content="Python documentation" />11<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, ..." />12<meta property="og:image:width" content="1146" />13<meta property="og:image:height" content="600" />14<meta property="og:image" content="https://docs.python.org/3.15/_images/social_previews/summary_library_inspect_6b999cc3.png" />15<meta property="og:image:alt" 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, ..." />16<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, ..." />17<meta name="twitter:card" content="summary_large_image" />18<meta name="theme-color" content="#3776ab">19 20 <title>inspect — Inspect live objects — Python 3.15.0a6 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">21 22 <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=b86133f3" />23 <link rel="stylesheet" type="text/css" href="../_static/classic.css?v=234b1a7c" />24 <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=89a2f22a" />25 <link rel="stylesheet" type="text/css" href="../_static/profiling-sampling-visualization.css?v=0c2600ae" />26 <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=5349f25f" />27 28 <script src="../_static/documentation_options.js?v=6b7c9ff5"></script>29 <script src="../_static/doctools.js?v=9bcbadda"></script>30 <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>31 <script src="../_static/profiling-sampling-visualization.js?v=9811ed04"></script>32 33 <script src="../_static/sidebar.js"></script>34 35 <link rel="search" type="application/opensearchdescription+xml"36 title="Search within Python 3.15.0a6 documentation"37 href="../_static/opensearch.xml"/>38 <link rel="author" title="About these documents" href="../about.html" />39 <link rel="index" title="Index" href="../genindex.html" />40 <link rel="search" title="Search" href="../search.html" />41 <link rel="copyright" title="Copyright" href="../copyright.html" />42 <link rel="next" title="annotationlib — Functionality for introspecting annotations" href="annotationlib.html" />43 <link rel="prev" title="gc — Garbage Collector interface" href="gc.html" />44 45 46 <script defer file-types="bz2,epub,zip" data-domain="docs.python.org" src="https://analytics.python.org/js/script.file-downloads.outbound-links.js"></script>47 48 <link rel="canonical" href="https://docs.python.org/3/library/inspect.html">49 50 51 52 53 <style>54 @media only screen {55 table.full-width-table {56 width: 100%;57 }58 }59 </style>60<link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">61 <link rel="shortcut icon" type="image/png" href="../_static/py.svg">62 <script type="text/javascript" src="../_static/copybutton.js"></script>63 <script type="text/javascript" src="../_static/menu.js"></script>64 <script type="text/javascript" src="../_static/search-focus.js"></script>65 <script type="text/javascript" src="../_static/themetoggle.js"></script> 66 <script type="text/javascript" src="../_static/rtd_switcher.js"></script>67 <meta name="readthedocs-addons-api-version" content="1">68 69 </head>70<body>71<div class="mobile-nav">72 <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"73 aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu">74 <nav class="nav-content" role="navigation">75 <label for="menuToggler" class="toggler__label">76 <span></span>77 </label>78 <span class="nav-items-wrapper">79 <a href="https://www.python.org/" class="nav-logo">80 <img src="../_static/py.svg" alt="Python logo">81 </a>82 <span class="version_switcher_placeholder"></span>83 <form role="search" class="search" action="../search.html" method="get">84 <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">85 <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>86 </svg>87 <input placeholder="Quick search" aria-label="Quick search" type="search" name="q">88 <input type="submit" value="Go">89 </form>90 </span>91 </nav>92 <div class="menu-wrapper">93 <nav class="menu" role="navigation" aria-label="main navigation">94 <div class="language_switcher_placeholder"></div>95 96<label class="theme-selector-label">97 Theme98 <select class="theme-selector" oninput="activateTheme(this.value)">99 <option value="auto" selected>Auto</option>100 <option value="light">Light</option>101 <option value="dark">Dark</option>102 </select>103</label>104 <div>105 <h3><a href="../contents.html">Table of Contents</a></h3>106 <ul>107<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>108<li><a class="reference internal" href="#types-and-members">Types and members</a></li>109<li><a class="reference internal" href="#retrieving-source-code">Retrieving source code</a></li>110<li><a class="reference internal" href="#introspecting-callables-with-the-signature-object">Introspecting callables with the Signature object</a></li>111<li><a class="reference internal" href="#classes-and-functions">Classes and functions</a></li>112<li><a class="reference internal" href="#the-interpreter-stack">The interpreter stack</a></li>113<li><a class="reference internal" href="#fetching-attributes-statically">Fetching attributes statically</a></li>114<li><a class="reference internal" href="#current-state-of-generators-coroutines-and-asynchronous-generators">Current State of Generators, Coroutines, and Asynchronous Generators</a></li>115<li><a class="reference internal" href="#code-objects-bit-flags">Code Objects Bit Flags</a></li>116<li><a class="reference internal" href="#buffer-flags">Buffer flags</a></li>117<li><a class="reference internal" href="#command-line-interface">Command-line interface</a></li>118</ul>119</li>120</ul>121 122 </div>123 <div>124 <h4>Previous topic</h4>125 <p class="topless"><a href="gc.html"126 title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gc</span></code> — Garbage Collector interface</a></p>127 </div>128 <div>129 <h4>Next topic</h4>130 <p class="topless"><a href="annotationlib.html"131 title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">annotationlib</span></code> — Functionality for introspecting annotations</a></p>132 </div>133 <script>134 document.addEventListener('DOMContentLoaded', () => {135 const title = document.querySelector('meta[property="og:title"]').content;136 const elements = document.querySelectorAll('.improvepage');137 const pageurl = window.location.href.split('?')[0];138 elements.forEach(element => {139 const url = new URL(element.href.split('?')[0].replace("-nojs", ""));140 url.searchParams.set('pagetitle', title);141 url.searchParams.set('pageurl', pageurl);142 url.searchParams.set('pagesource', "library/inspect.rst");143 element.href = url.toString();144 });145 });146 </script>147 <div role="note" aria-label="source link">148 <h3>This page</h3>149 <ul class="this-page-menu">150 <li><a href="../bugs.html">Report a bug</a></li>151 <li><a class="improvepage" href="../improve-page-nojs.html">Improve this page</a></li>152 <li>153 <a href="https://github.com/python/cpython/blob/main/Doc/library/inspect.rst?plain=1"154 rel="nofollow">Show source155 </a>156 </li>157 158 </ul>159 </div>160 </nav>161 </div>162</div>163 164 165 <div class="related" role="navigation" aria-label="Related">166 <h3>Navigation</h3>167 <ul>168 <li class="right" style="margin-right: 10px">169 <a href="../genindex.html" title="General Index"170 accesskey="I">index</a></li>171 <li class="right" >172 <a href="../py-modindex.html" title="Python Module Index"173 >modules</a> |</li>174 <li class="right" >175 <a href="annotationlib.html" title="annotationlib — Functionality for introspecting annotations"176 accesskey="N">next</a> |</li>177 <li class="right" >178 <a href="gc.html" title="gc — Garbage Collector interface"179 accesskey="P">previous</a> |</li>180 181 <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"></li>182 <li><a href="https://www.python.org/">Python</a> »</li>183 <li class="switchers">184 <div class="language_switcher_placeholder"></div>185 <div class="version_switcher_placeholder"></div>186 </li>187 <li>188 189 </li>190 <li id="cpython-language-and-version">191 <a href="../index.html">3.15.0a6 Documentation</a> »192 </li>193 194 <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>195 <li class="nav-item nav-item-2"><a href="python.html" accesskey="U">Python Runtime Services</a> »</li>196 <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>197 <li class="right">198 199 200 <div class="inline-search" role="search">201 <form class="inline-search" action="../search.html" method="get">202 <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box">203 <input type="submit" value="Go">204 </form>205 </div>206 |207 </li>208 <li class="right">209<label class="theme-selector-label">210 Theme211 <select class="theme-selector" oninput="activateTheme(this.value)">212 <option value="auto" selected>Auto</option>213 <option value="light">Light</option>214 <option value="dark">Dark</option>215 </select>216</label> |</li>217 218 </ul>219 </div> 220 221 <div class="document">222 <div class="documentwrapper">223 <div class="bodywrapper">224 <div class="body" role="main">225 226 <section id="inspect-inspect-live-objects">227<h1><code class="xref py py-mod docutils literal notranslate"><span class="pre">inspect</span></code> — Inspect live objects<a class="headerlink" href="#inspect-inspect-live-objects" title="Link to this heading">¶</a></h1>228<p id="module-inspect"><strong>Source code:</strong> <a class="extlink-source reference external" href="https://github.com/python/cpython/tree/main/Lib/inspect.py">Lib/inspect.py</a></p>229<hr class="docutils" />230<p>The <code class="xref py py-mod docutils literal notranslate"><span class="pre">inspect</span></code> module provides several useful functions to help get231information about live objects such as modules, classes, methods, functions,232tracebacks, frame objects, and code objects. For example, it can help you233examine the contents of a class, retrieve the source code of a method, extract234and format the argument list for a function, or get all the information you need235to display a detailed traceback.</p>236<p>There are four main kinds of services provided by this module: type checking,237getting source code, inspecting classes and functions, and examining the238interpreter stack.</p>239<section id="types-and-members">240<span id="inspect-types"></span><h2>Types and members<a class="headerlink" href="#types-and-members" title="Link to this heading">¶</a></h2>241<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 a242class or module. The functions whose names begin with “is” are mainly243provided as convenient choices for the second argument to <code class="xref py py-func docutils literal notranslate"><span class="pre">getmembers()</span></code>.244They also help you determine when you can expect to find the following special245attributes (see <a class="reference internal" href="../reference/datamodel.html#import-mod-attrs"><span class="std std-ref">Import-related attributes on module objects</span></a> for module attributes):</p>246<table class="docutils align-default">247<thead>248<tr class="row-odd"><th class="head"><p>Type</p></th>249<th class="head"><p>Attribute</p></th>250<th class="head"><p>Description</p></th>251</tr>252</thead>253<tbody>254<tr class="row-even"><td><p>class</p></td>255<td><p>__doc__</p></td>256<td><p>documentation string</p></td>257</tr>258<tr class="row-odd"><td></td>259<td><p>__name__</p></td>260<td><p>name with which this261class was defined</p></td>262</tr>263<tr class="row-even"><td></td>264<td><p>__qualname__</p></td>265<td><p>qualified name</p></td>266</tr>267<tr class="row-odd"><td></td>268<td><p>__module__</p></td>269<td><p>name of module in which270this class was defined</p></td>271</tr>272<tr class="row-even"><td></td>273<td><p>__type_params__</p></td>274<td><p>A tuple containing the275<a class="reference internal" href="../reference/compound_stmts.html#type-params"><span class="std std-ref">type parameters</span></a> of276a generic class</p></td>277</tr>278<tr class="row-odd"><td><p>method</p></td>279<td><p>__doc__</p></td>280<td><p>documentation string</p></td>281</tr>282<tr class="row-even"><td></td>283<td><p>__name__</p></td>284<td><p>name with which this285method was defined</p></td>286</tr>287<tr class="row-odd"><td></td>288<td><p>__qualname__</p></td>289<td><p>qualified name</p></td>290</tr>291<tr class="row-even"><td></td>292<td><p>__func__</p></td>293<td><p>function object294containing implementation295of method</p></td>296</tr>297<tr class="row-odd"><td></td>298<td><p>__self__</p></td>299<td><p>instance to which this300method is bound, or301<code class="docutils literal notranslate"><span class="pre">None</span></code></p></td>302</tr>303<tr class="row-even"><td></td>304<td><p>__module__</p></td>305<td><p>name of module in which306this method was defined</p></td>307</tr>308<tr class="row-odd"><td><p>function</p></td>309<td><p>__doc__</p></td>310<td><p>documentation string</p></td>311</tr>312<tr class="row-even"><td></td>313<td><p>__name__</p></td>314<td><p>name with which this315function was defined</p></td>316</tr>317<tr class="row-odd"><td></td>318<td><p>__qualname__</p></td>319<td><p>qualified name</p></td>320</tr>321<tr class="row-even"><td></td>322<td><p>__code__</p></td>323<td><p>code object containing324compiled function325<a class="reference internal" href="../glossary.html#term-bytecode"><span class="xref std std-term">bytecode</span></a></p></td>326</tr>327<tr class="row-odd"><td></td>328<td><p>__defaults__</p></td>329<td><p>tuple of any default330values for positional or331keyword parameters</p></td>332</tr>333<tr class="row-even"><td></td>334<td><p>__kwdefaults__</p></td>335<td><p>mapping of any default336values for keyword-only337parameters</p></td>338</tr>339<tr class="row-odd"><td></td>340<td><p>__globals__</p></td>341<td><p>global namespace in which342this function was defined</p></td>343</tr>344<tr class="row-even"><td></td>345<td><p>__builtins__</p></td>346<td><p>builtins namespace</p></td>347</tr>348<tr class="row-odd"><td></td>349<td><p>__annotations__</p></td>350<td><p>mapping of parameters351names to annotations;352<code class="docutils literal notranslate"><span class="pre">"return"</span></code> key is353reserved for return354annotations.</p></td>355</tr>356<tr class="row-even"><td></td>357<td><p>__type_params__</p></td>358<td><p>A tuple containing the359<a class="reference internal" href="../reference/compound_stmts.html#type-params"><span class="std std-ref">type parameters</span></a> of360a generic function</p></td>361</tr>362<tr class="row-odd"><td></td>363<td><p>__module__</p></td>364<td><p>name of module in which365this function was defined</p></td>366</tr>367<tr class="row-even"><td><p>traceback</p></td>368<td><p>tb_frame</p></td>369<td><p>frame object at this370level</p></td>371</tr>372<tr class="row-odd"><td></td>373<td><p>tb_lasti</p></td>374<td><p>index of last attempted375instruction in bytecode</p></td>376</tr>377<tr class="row-even"><td></td>378<td><p>tb_lineno</p></td>379<td><p>current line number in380Python source code</p></td>381</tr>382<tr class="row-odd"><td></td>383<td><p>tb_next</p></td>384<td><p>next inner traceback385object (called by this386level)</p></td>387</tr>388<tr class="row-even"><td><p>frame</p></td>389<td><p>f_back</p></td>390<td><p>next outer frame object391(this frame’s caller)</p></td>392</tr>393<tr class="row-odd"><td></td>394<td><p>f_builtins</p></td>395<td><p>builtins namespace seen396by this frame</p></td>397</tr>398<tr class="row-even"><td></td>399<td><p>f_code</p></td>400<td><p>code object being401executed in this frame</p></td>402</tr>403<tr class="row-odd"><td></td>404<td><p>f_globals</p></td>405<td><p>global namespace seen by406this frame</p></td>407</tr>408<tr class="row-even"><td></td>409<td><p>f_lasti</p></td>410<td><p>index of last attempted411instruction in bytecode</p></td>412</tr>413<tr class="row-odd"><td></td>414<td><p>f_lineno</p></td>415<td><p>current line number in416Python source code</p></td>417</tr>418<tr class="row-even"><td></td>419<td><p>f_locals</p></td>420<td><p>local namespace seen by421this frame</p></td>422</tr>423<tr class="row-odd"><td></td>424<td><p>f_generator</p></td>425<td><p>returns the generator or426coroutine object that427owns this frame, or428<code class="docutils literal notranslate"><span class="pre">None</span></code> if the frame is429of a regular function</p></td>430</tr>431<tr class="row-even"><td></td>432<td><p>f_trace</p></td>433<td><p>tracing function for this434frame, or <code class="docutils literal notranslate"><span class="pre">None</span></code></p></td>435</tr>436<tr class="row-odd"><td></td>437<td><p>f_trace_lines</p></td>438<td><p>indicate whether a439tracing event is440triggered for each source441source line</p></td>442</tr>443<tr class="row-even"><td></td>444<td><p>f_trace_opcodes</p></td>445<td><p>indicate whether446per-opcode events are447requested</p></td>448</tr>449<tr class="row-odd"><td></td>450<td><p>clear()</p></td>451<td><p>used to clear all452references to local453variables</p></td>454</tr>455<tr class="row-even"><td><p>code</p></td>456<td><p>co_argcount</p></td>457<td><p>number of arguments (not458including keyword only459arguments, * or **460args)</p></td>461</tr>462<tr class="row-odd"><td></td>463<td><p>co_code</p></td>464<td><p>string of raw compiled465bytecode</p></td>466</tr>467<tr class="row-even"><td></td>468<td><p>co_cellvars</p></td>469<td><p>tuple of names of cell470variables (referenced by471containing scopes)</p></td>472</tr>473<tr class="row-odd"><td></td>474<td><p>co_consts</p></td>475<td><p>tuple of constants used476in the bytecode</p></td>477</tr>478<tr class="row-even"><td></td>479<td><p>co_filename</p></td>480<td><p>name of file in which481this code object was482created</p></td>483</tr>484<tr class="row-odd"><td></td>485<td><p>co_firstlineno</p></td>486<td><p>number of first line in487Python source code</p></td>488</tr>489<tr class="row-even"><td></td>490<td><p>co_flags</p></td>491<td><p>bitmap of <code class="docutils literal notranslate"><span class="pre">CO_*</span></code> flags,492read more <a class="reference internal" href="#inspect-module-co-flags"><span class="std std-ref">here</span></a></p></td>493</tr>494<tr class="row-odd"><td></td>495<td><p>co_lnotab</p></td>496<td><p>encoded mapping of line497numbers to bytecode498indices</p></td>499</tr>500<tr class="row-even"><td></td>501<td><p>co_freevars</p></td>502<td><p>tuple of names of free503variables (referenced via504a function’s closure)</p></td>505</tr>506<tr class="row-odd"><td></td>507<td><p>co_posonlyargcount</p></td>508<td><p>number of positional only509arguments</p></td>510</tr>511<tr class="row-even"><td></td>512<td><p>co_kwonlyargcount</p></td>513<td><p>number of keyword only514arguments (not including515** arg)</p></td>516</tr>517<tr class="row-odd"><td></td>518<td><p>co_name</p></td>519<td><p>name with which this code520object was defined</p></td>521</tr>522<tr class="row-even"><td></td>523<td><p>co_qualname</p></td>524<td><p>fully qualified name with525which this code object526was defined</p></td>527</tr>528<tr class="row-odd"><td></td>529<td><p>co_names</p></td>530<td><p>tuple of names other531than arguments and532function locals</p></td>533</tr>534<tr class="row-even"><td></td>535<td><p>co_nlocals</p></td>536<td><p>number of local variables</p></td>537</tr>538<tr class="row-odd"><td></td>539<td><p>co_stacksize</p></td>540<td><p>virtual machine stack541space required</p></td>542</tr>543<tr class="row-even"><td></td>544<td><p>co_varnames</p></td>545<td><p>tuple of names of546arguments and local547variables</p></td>548</tr>549<tr class="row-odd"><td></td>550<td><p>co_lines()</p></td>551<td><p>returns an iterator that552yields successive553bytecode ranges</p></td>554</tr>555<tr class="row-even"><td></td>556<td><p>co_positions()</p></td>557<td><p>returns an iterator of558source code positions for559each bytecode instruction</p></td>560</tr>561<tr class="row-odd"><td></td>562<td><p>replace()</p></td>563<td><p>returns a copy of the564code object with new565values</p></td>566</tr>567<tr class="row-even"><td><p>generator</p></td>568<td><p>__name__</p></td>569<td><p>name</p></td>570</tr>571<tr class="row-odd"><td></td>572<td><p>__qualname__</p></td>573<td><p>qualified name</p></td>574</tr>575<tr class="row-even"><td></td>576<td><p>gi_frame</p></td>577<td><p>frame</p></td>578</tr>579<tr class="row-odd"><td></td>580<td><p>gi_running</p></td>581<td><p>is the generator running?</p></td>582</tr>583<tr class="row-even"><td></td>584<td><p>gi_suspended</p></td>585<td><p>is the generator586suspended?</p></td>587</tr>588<tr class="row-odd"><td></td>589<td><p>gi_code</p></td>590<td><p>code</p></td>591</tr>592<tr class="row-even"><td></td>593<td><p>gi_yieldfrom</p></td>594<td><p>object being iterated by595<code class="docutils literal notranslate"><span class="pre">yield</span> <span class="pre">from</span></code>, or596<code class="docutils literal notranslate"><span class="pre">None</span></code></p></td>597</tr>598<tr class="row-odd"><td></td>599<td><p>gi_state</p></td>600<td><p>state of the generator,601one of <code class="docutils literal notranslate"><span class="pre">GEN_CREATED</span></code>,602<code class="docutils literal notranslate"><span class="pre">GEN_RUNNING</span></code>,603<code class="docutils literal notranslate"><span class="pre">GEN_SUSPENDED</span></code>, or604<code class="docutils literal notranslate"><span class="pre">GEN_CLOSED</span></code></p></td>605</tr>606<tr class="row-even"><td><p>async generator</p></td>607<td><p>__name__</p></td>608<td><p>name</p></td>609</tr>610<tr class="row-odd"><td></td>611<td><p>__qualname__</p></td>612<td><p>qualified name</p></td>613</tr>614<tr class="row-even"><td></td>615<td><p>ag_await</p></td>616<td><p>object being awaited on,617or <code class="docutils literal notranslate"><span class="pre">None</span></code></p></td>618</tr>619<tr class="row-odd"><td></td>620<td><p>ag_frame</p></td>621<td><p>frame</p></td>622</tr>623<tr class="row-even"><td></td>624<td><p>ag_running</p></td>625<td><p>is the generator running?</p></td>626</tr>627<tr class="row-odd"><td></td>628<td><p>ag_suspended</p></td>629<td><p>is the generator630suspended?</p></td>631</tr>632<tr class="row-even"><td></td>633<td><p>ag_code</p></td>634<td><p>code</p></td>635</tr>636<tr class="row-odd"><td></td>637<td><p>ag_state</p></td>638<td><p>state of the async639generator, one of640<code class="docutils literal notranslate"><span class="pre">AGEN_CREATED</span></code>,641<code class="docutils literal notranslate"><span class="pre">AGEN_RUNNING</span></code>,642<code class="docutils literal notranslate"><span class="pre">AGEN_SUSPENDED</span></code>, or643<code class="docutils literal notranslate"><span class="pre">AGEN_CLOSED</span></code></p></td>644</tr>645<tr class="row-even"><td><p>coroutine</p></td>646<td><p>__name__</p></td>647<td><p>name</p></td>648</tr>649<tr class="row-odd"><td></td>650<td><p>__qualname__</p></td>651<td><p>qualified name</p></td>652</tr>653<tr class="row-even"><td></td>654<td><p>cr_await</p></td>655<td><p>object being awaited on,656or <code class="docutils literal notranslate"><span class="pre">None</span></code></p></td>657</tr>658<tr class="row-odd"><td></td>659<td><p>cr_frame</p></td>660<td><p>frame</p></td>661</tr>662<tr class="row-even"><td></td>663<td><p>cr_running</p></td>664<td><p>is the coroutine running?</p></td>665</tr>666<tr class="row-odd"><td></td>667<td><p>cr_suspended</p></td>668<td><p>is the coroutine669suspended?</p></td>670</tr>671<tr class="row-even"><td></td>672<td><p>cr_code</p></td>673<td><p>code</p></td>674</tr>675<tr class="row-odd"><td></td>676<td><p>cr_origin</p></td>677<td><p>where coroutine was678created, or <code class="docutils literal notranslate"><span class="pre">None</span></code>. See679<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>680</tr>681<tr class="row-even"><td></td>682<td><p>cr_state</p></td>683<td><p>state of the coroutine,684one of <code class="docutils literal notranslate"><span class="pre">CORO_CREATED</span></code>,685<code class="docutils literal notranslate"><span class="pre">CORO_RUNNING</span></code>,686<code class="docutils literal notranslate"><span class="pre">CORO_SUSPENDED</span></code>, or687<code class="docutils literal notranslate"><span class="pre">CORO_CLOSED</span></code></p></td>688</tr>689<tr class="row-odd"><td><p>builtin</p></td>690<td><p>__doc__</p></td>691<td><p>documentation string</p></td>692</tr>693<tr class="row-even"><td></td>694<td><p>__name__</p></td>695<td><p>original name of this696function or method</p></td>697</tr>698<tr class="row-odd"><td></td>699<td><p>__qualname__</p></td>700<td><p>qualified name</p></td>701</tr>702<tr class="row-even"><td></td>703<td><p>__self__</p></td>704<td><p>instance to which a705method is bound, or706<code class="docutils literal notranslate"><span class="pre">None</span></code></p></td>707</tr>708</tbody>709</table>710<div class="versionchanged">711<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>712<p>The <code class="docutils literal notranslate"><span class="pre">__name__</span></code> attribute of generators is now set from the function713name, instead of the code name, and it can now be modified.</p>714</div>715<div class="versionchanged">716<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>717</div>718<div class="versionchanged">719<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>720</div>721<div class="versionchanged">722<p><span class="versionmodified changed">Changed in version 3.11: </span>Add <code class="docutils literal notranslate"><span class="pre">gi_suspended</span></code> attribute to generators.</p>723</div>724<div class="versionchanged">725<p><span class="versionmodified changed">Changed in version 3.11: </span>Add <code class="docutils literal notranslate"><span class="pre">cr_suspended</span></code> attribute to coroutines.</p>726</div>727<div class="versionchanged">728<p><span class="versionmodified changed">Changed in version 3.12: </span>Add <code class="docutils literal notranslate"><span class="pre">ag_suspended</span></code> attribute to async generators.</p>729</div>730<div class="versionchanged">731<p><span class="versionmodified changed">Changed in version 3.14: </span>Add <code class="docutils literal notranslate"><span class="pre">f_generator</span></code> attribute to frames.</p>732</div>733<div class="versionchanged">734<p><span class="versionmodified changed">Changed in version 3.15: </span>Add <code class="docutils literal notranslate"><span class="pre">gi_state</span></code> attribute to generators, <code class="docutils literal notranslate"><span class="pre">cr_state</span></code> attribute to735coroutines, and <code class="docutils literal notranslate"><span class="pre">ag_state</span></code> attribute to async generators.</p>736</div>737<dl class="py function">738<dt class="sig sig-object py" id="inspect.getmembers">739<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>740<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>741pairs sorted by name. If the optional <em>predicate</em> argument—which will be742called with the <code class="docutils literal notranslate"><span class="pre">value</span></code> object of each member—is supplied, only members743for which the predicate returns a true value are included.</p>744<div class="admonition note">745<p class="admonition-title">Note</p>746<p><code class="xref py py-func docutils literal notranslate"><span class="pre">getmembers()</span></code> will only return class attributes defined in the747metaclass when the argument is a class and those attributes have been748listed 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>749</div>750</dd></dl>751 752<dl class="py function">753<dt class="sig sig-object py" id="inspect.getmembers_static">754<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>755<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>756pairs sorted by name without triggering dynamic lookup via the descriptor757protocol, __getattr__ or __getattribute__. Optionally, only return members758that satisfy a given predicate.</p>759<div class="admonition note">760<p class="admonition-title">Note</p>761<p><code class="xref py py-func docutils literal notranslate"><span class="pre">getmembers_static()</span></code> may not be able to retrieve all members762that getmembers can fetch (like dynamically created attributes)763and may find members that getmembers can’t (like descriptors764that raise AttributeError). It can also return descriptor objects765instead of instance members in some cases.</p>766</div>767<div class="versionadded">768<p><span class="versionmodified added">Added in version 3.11.</span></p>769</div>770</dd></dl>771 772<dl class="py function">773<dt class="sig sig-object py" id="inspect.getmodulename">774<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>775<dd><p>Return the name of the module named by the file <em>path</em>, without including the776names of enclosing packages. The file extension is checked against all of777the 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,778the final path component is returned with the extension removed.779Otherwise, <code class="docutils literal notranslate"><span class="pre">None</span></code> is returned.</p>780<p>Note that this function <em>only</em> returns a meaningful name for actual781Python modules - paths that potentially refer to Python packages will782still return <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>783<div class="versionchanged">784<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>785</div>786</dd></dl>787 788<dl class="py function">789<dt class="sig sig-object py" id="inspect.ismodule">790<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>791<dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the object is a module.</p>792</dd></dl>793 794<dl class="py function">795<dt class="sig sig-object py" id="inspect.isclass">796<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>797<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 Python798code.</p>799</dd></dl>800 801<dl class="py function">802<dt class="sig sig-object py" id="inspect.ismethod">803<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>804<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>805</dd></dl>806 807<dl class="py function">808<dt class="sig sig-object py" id="inspect.ispackage">809<span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">ispackage</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.ispackage" title="Link to this definition">¶</a></dt>810<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-package"><span class="xref std std-term">package</span></a>.</p>811<div class="versionadded">812<p><span class="versionmodified added">Added in version 3.14.</span></p>813</div>814</dd></dl>815 816<dl class="py function">817<dt class="sig sig-object py" id="inspect.isfunction">818<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>819<dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the object is a Python function, which includes functions820created by a <a class="reference internal" href="../glossary.html#term-lambda"><span class="xref std std-term">lambda</span></a> expression.</p>821</dd></dl>822 823<dl class="py function">824<dt class="sig sig-object py" id="inspect.isgeneratorfunction">825<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>826<dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the object is a Python generator function.</p>827<div class="versionchanged">828<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 the829wrapped function is a Python generator function.</p>830</div>831<div class="versionchanged">832<p><span class="versionmodified changed">Changed in version 3.13: </span>Functions wrapped in <a class="reference internal" href="functools.html#functools.partialmethod" title="functools.partialmethod"><code class="xref py py-func docutils literal notranslate"><span class="pre">functools.partialmethod()</span></code></a> now return <code class="docutils literal notranslate"><span class="pre">True</span></code>833if the wrapped function is a Python generator function.</p>834</div>835</dd></dl>836 837<dl class="py function">838<dt class="sig sig-object py" id="inspect.isgenerator">839<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>840<dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the object is a generator.</p>841</dd></dl>842 843<dl class="py function">844<dt class="sig sig-object py" id="inspect.iscoroutinefunction">845<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>846<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 function847defined 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>848wrapping a <span class="xref std std-term">coroutine function</span>, or a sync function marked with849<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>850<div class="versionadded">851<p><span class="versionmodified added">Added in version 3.5.</span></p>852</div>853<div class="versionchanged">854<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 the855wrapped function is a <a class="reference internal" href="../glossary.html#term-coroutine-function"><span class="xref std std-term">coroutine function</span></a>.</p>856</div>857<div class="versionchanged">858<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 return859<code class="docutils literal notranslate"><span class="pre">True</span></code>.</p>860</div>861<div class="versionchanged">862<p><span class="versionmodified changed">Changed in version 3.13: </span>Functions wrapped in <a class="reference internal" href="functools.html#functools.partialmethod" title="functools.partialmethod"><code class="xref py py-func docutils literal notranslate"><span class="pre">functools.partialmethod()</span></code></a> now return <code class="docutils literal notranslate"><span class="pre">True</span></code>863if the 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>864</div>865</dd></dl>866 867<dl class="py function">868<dt class="sig sig-object py" id="inspect.markcoroutinefunction">869<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>870<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 not871otherwise 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>872<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>, if873the 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>874<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. Also875acceptable is calling the function and testing the return with876<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>877<div class="versionadded">878<p><span class="versionmodified added">Added in version 3.12.</span></p>879</div>880</dd></dl>881 882<dl class="py function">883<dt class="sig sig-object py" id="inspect.iscoroutine">884<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>885<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 an886<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>887<div class="versionadded">888<p><span class="versionmodified added">Added in version 3.5.</span></p>889</div>890</dd></dl>891 892<dl class="py function">893<dt class="sig sig-object py" id="inspect.isawaitable">894<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>895<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>896<p>Can also be used to distinguish generator-based coroutines from regular897generators:</p>898<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span><span class="w"> </span><span class="nn">types</span>899 900<span class="k">def</span><span class="w"> </span><span class="nf">gen</span><span class="p">():</span>901 <span class="k">yield</span>902<span class="nd">@types</span><span class="o">.</span><span class="n">coroutine</span>903<span class="k">def</span><span class="w"> </span><span class="nf">gen_coro</span><span class="p">():</span>904 <span class="k">yield</span>905 906<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>907<span class="k">assert</span> <span class="n">isawaitable</span><span class="p">(</span><span class="n">gen_coro</span><span class="p">())</span>908</pre></div>909</div>910<div class="versionadded">911<p><span class="versionmodified added">Added in version 3.5.</span></p>912</div>913</dd></dl>914 915<dl class="py function">916<dt class="sig sig-object py" id="inspect.isasyncgenfunction">917<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>918<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,919for example:</p>920<div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">async</span> <span class="k">def</span><span class="w"> </span><span class="nf">agen</span><span class="p">():</span>921<span class="gp">... </span> <span class="k">yield</span> <span class="mi">1</span>922<span class="gp">...</span>923<span class="gp">>>> </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>924<span class="go">True</span>925</pre></div>926</div>927<div class="versionadded">928<p><span class="versionmodified added">Added in version 3.6.</span></p>929</div>930<div class="versionchanged">931<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 the932wrapped function is an <a class="reference internal" href="../glossary.html#term-asynchronous-generator"><span class="xref std std-term">asynchronous generator</span></a> function.</p>933</div>934<div class="versionchanged">935<p><span class="versionmodified changed">Changed in version 3.13: </span>Functions wrapped in <a class="reference internal" href="functools.html#functools.partialmethod" title="functools.partialmethod"><code class="xref py py-func docutils literal notranslate"><span class="pre">functools.partialmethod()</span></code></a> now return <code class="docutils literal notranslate"><span class="pre">True</span></code>936if the 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>937</div>938</dd></dl>939 940<dl class="py function">941<dt class="sig sig-object py" id="inspect.isasyncgen">942<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>943<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>944created by an <a class="reference internal" href="../glossary.html#term-asynchronous-generator"><span class="xref std std-term">asynchronous generator</span></a> function.</p>945<div class="versionadded">946<p><span class="versionmodified added">Added in version 3.6.</span></p>947</div>948</dd></dl>949 950<dl class="py function">951<dt class="sig sig-object py" id="inspect.istraceback">952<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>953<dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the object is a traceback.</p>954</dd></dl>955 956<dl class="py function">957<dt class="sig sig-object py" id="inspect.isframe">958<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>959<dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the object is a frame.</p>960</dd></dl>961 962<dl class="py function">963<dt class="sig sig-object py" id="inspect.iscode">964<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>965<dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the object is a code.</p>966</dd></dl>967 968<dl class="py function">969<dt class="sig sig-object py" id="inspect.isbuiltin">970<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>971<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>972</dd></dl>973 974<dl class="py function">975<dt class="sig sig-object py" id="inspect.ismethodwrapper">976<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>977<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>978<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>,979<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>980<div class="versionadded">981<p><span class="versionmodified added">Added in version 3.11.</span></p>982</div>983</dd></dl>984 985<dl class="py function">986<dt class="sig sig-object py" id="inspect.isroutine">987<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>988<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>989</dd></dl>990 991<dl class="py function">992<dt class="sig sig-object py" id="inspect.isabstract">993<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>994<dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the object is an abstract base class.</p>995</dd></dl>996 997<dl class="py function">998<dt class="sig sig-object py" id="inspect.ismethoddescriptor">999<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>1000<dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the object is a method descriptor, but not if1001<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>1002are true.</p>1003<p>This, for example, is true of <code class="docutils literal notranslate"><span class="pre">int.__add__</span></code>. An object passing this test1004has 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>1005method or a <a class="reference internal" href="../reference/datamodel.html#object.__delete__" title="object.__delete__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__delete__()</span></code></a> method. Beyond that, the set of1006attributes varies. A <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 usually1007sensible, and <a class="reference internal" href="stdtypes.html#definition.__doc__" title="definition.__doc__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__doc__</span></code></a> often is.</p>1008<p>Methods implemented via descriptors that also pass one of the other tests1009return <code class="docutils literal notranslate"><span class="pre">False</span></code> from the <code class="xref py py-func docutils literal notranslate"><span class="pre">ismethoddescriptor()</span></code> test, simply because the1010other tests promise more – you can, e.g., count on having the1011<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 passes1012<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>1013<div class="versionchanged">1014<p><span class="versionmodified changed">Changed in version 3.13: </span>This function no longer incorrectly reports objects with <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>1015and <a class="reference internal" href="../reference/datamodel.html#object.__delete__" title="object.__delete__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__delete__()</span></code></a>, but not <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>, as being method1016descriptors (such objects are data descriptors, not method descriptors).</p>1017</div>1018</dd></dl>1019 1020<dl class="py function">1021<dt class="sig sig-object py" id="inspect.isdatadescriptor">1022<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>1023<dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the object is a data descriptor.</p>1024<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.1025Examples are properties (defined in Python), getsets, and members. The1026latter two are defined in C and there are more specific tests available for1027those types, which is robust across Python implementations. Typically, data1028descriptors 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> attributes1029(properties, getsets, and members have both of these attributes), but this is1030not guaranteed.</p>1031</dd></dl>1032 1033<dl class="py function">1034<dt class="sig sig-object py" id="inspect.isgetsetdescriptor">1035<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>1036<dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the object is a getset descriptor.</p>1037<div class="impl-detail compound">1038<p><strong>CPython implementation detail:</strong> getsets are attributes defined in extension modules via1039<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 such1040types, this method will always return <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p>1041</div>1042</dd></dl>1043 1044<dl class="py function">1045<dt class="sig sig-object py" id="inspect.ismemberdescriptor">1046<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>1047<dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the object is a member descriptor.</p>1048<div class="impl-detail compound">1049<p><strong>CPython implementation detail:</strong> Member descriptors are attributes defined in extension modules via1050<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 such1051types, this method will always return <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p>1052</div>1053</dd></dl>1054 1055</section>1056<section id="retrieving-source-code">1057<span id="inspect-source"></span><h2>Retrieving source code<a class="headerlink" href="#retrieving-source-code" title="Link to this heading">¶</a></h2>1058<dl class="py function">1059<dt class="sig sig-object py" id="inspect.getdoc">1060<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>, <em class="sig-param"><span class="keyword-only-separator o"><abbr title="Keyword-only parameters separator (PEP 3102)"><span class="pre">*</span></abbr></span></em>, <em class="sig-param"><span class="n"><span class="pre">inherit_class_doc</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">fallback_to_class_doc</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.getdoc" title="Link to this definition">¶</a></dt>1061<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>.1062If the documentation string for an object is not provided:</p>1063<ul class="simple">1064<li><p>if the object is a class and <em>inherit_class_doc</em> is true (by default),1065retrieve the documentation string from the inheritance hierarchy;</p></li>1066<li><p>if the object is a method, a property or a descriptor, retrieve1067the documentation string from the inheritance hierarchy;</p></li>1068<li><p>otherwise, if <em>fallback_to_class_doc</em> is true (by default), retrieve1069the documentation string from the class of the object.</p></li>1070</ul>1071<p>Return <code class="docutils literal notranslate"><span class="pre">None</span></code> if the documentation string is invalid or missing.</p>1072<div class="versionchanged">1073<p><span class="versionmodified changed">Changed in version 3.5: </span>Documentation strings are now inherited if not overridden.</p>1074</div>1075<div class="versionchanged">1076<p><span class="versionmodified changed">Changed in version 3.15: </span>Added parameters <em>inherit_class_doc</em> and <em>fallback_to_class_doc</em>.</p>1077<p>Documentation strings on <a class="reference internal" href="functools.html#functools.cached_property" title="functools.cached_property"><code class="xref py py-class docutils literal notranslate"><span class="pre">cached_property</span></code></a>1078objects are now inherited if not overridden.</p>1079</div>1080</dd></dl>1081 1082<dl class="py function">1083<dt class="sig sig-object py" id="inspect.getcomments">1084<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>1085<dd><p>Return in a single string any lines of comments immediately preceding the1086object’s source code (for a class, function, or method), or at the top of the1087Python source file (if the object is a module). If the object’s source code1088is unavailable, return <code class="docutils literal notranslate"><span class="pre">None</span></code>. This could happen if the object has been1089defined in C or the interactive shell.</p>1090</dd></dl>1091 1092<dl class="py function">1093<dt class="sig sig-object py" id="inspect.getfile">1094<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>1095<dd><p>Return the name of the (text or binary) file in which an object was defined.1096This 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,1097class, or function.</p>1098</dd></dl>1099 1100<dl class="py function">1101<dt class="sig sig-object py" id="inspect.getmodule">1102<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>1103<dd><p>Try to guess which module an object was defined in. Return <code class="docutils literal notranslate"><span class="pre">None</span></code>1104if the module cannot be determined.</p>1105</dd></dl>1106 1107<dl class="py function">1108<dt class="sig sig-object py" id="inspect.getsourcefile">1109<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>1110<dd><p>Return the name of the Python source file in which an object was defined1111or <code class="docutils literal notranslate"><span class="pre">None</span></code> if no way can be identified to get the source. This1112will 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, or1113function.</p>1114</dd></dl>1115 1116<dl class="py function">1117<dt class="sig sig-object py" id="inspect.getsourcelines">1118<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>1119<dd><p>Return a list of source lines and starting line number for an object. The1120argument may be a module, class, method, function, traceback, frame, or code1121object. The source code is returned as a list of the lines corresponding to the1122object and the line number indicates where in the original source file the first1123line 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 cannot1124be retrieved.1125A <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, or1126function.</p>1127<div class="versionchanged">1128<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 the1129former.</p>1130</div>1131</dd></dl>1132 1133<dl class="py function">1134<dt class="sig sig-object py" id="inspect.getsource">1135<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>1136<dd><p>Return the text of the source code for an object. The argument may be a module,1137class, method, function, traceback, frame, or code object. The source code is1138returned 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 code1139cannot be retrieved.1140A <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, or1141function.</p>1142<div class="versionchanged">1143<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 the1144former.</p>1145</div>1146</dd></dl>1147 1148<dl class="py function">1149<dt class="sig sig-object py" id="inspect.cleandoc">1150<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>1151<dd><p>Clean up indentation from docstrings that are indented to line up with blocks1152of code.</p>1153<p>All leading whitespace is removed from the first line. Any leading whitespace1154that can be uniformly removed from the second line onwards is removed. Empty1155lines at the beginning and end are subsequently removed. Also, all tabs are1156expanded to spaces.</p>1157</dd></dl>1158 1159</section>1160<section id="introspecting-callables-with-the-signature-object">1161<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>1162<div class="versionadded">1163<p><span class="versionmodified added">Added in version 3.3.</span></p>1164</div>1165<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 object1166and its return annotation. To retrieve a <code class="xref py py-class docutils literal notranslate"><span class="pre">Signature</span></code> object,1167use the <code class="xref py py-func docutils literal notranslate"><span class="pre">signature()</span></code>1168function.</p>1169<dl class="py function">1170<dt class="sig sig-object py" id="inspect.signature">1171<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="keyword-only-separator o"><abbr title="Keyword-only parameters separator (PEP 3102)"><span class="pre">*</span></abbr></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>, <em class="sig-param"><span class="n"><span class="pre">annotation_format</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">Format.VALUE</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.signature" title="Link to this definition">¶</a></dt>1172<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>1173<div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span><span class="w"> </span><span class="nn">inspect</span><span class="w"> </span><span class="kn">import</span> <span class="n">signature</span>1174<span class="gp">>>> </span><span class="k">def</span><span class="w"> </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>1175<span class="gp">... </span> <span class="k">pass</span>1176 1177<span class="gp">>>> </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>1178 1179<span class="gp">>>> </span><span class="nb">str</span><span class="p">(</span><span class="n">sig</span><span class="p">)</span>1180<span class="go">'(a, *, b: int, **kwargs)'</span>1181 1182<span class="gp">>>> </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">'b'</span><span class="p">])</span>1183<span class="go">'b: int'</span>1184 1185<span class="gp">>>> </span><span class="n">sig</span><span class="o">.</span><span class="n">parameters</span><span class="p">[</span><span class="s1">'b'</span><span class="p">]</span><span class="o">.</span><span class="n">annotation</span>1186<span class="go"><class 'int'></span>1187</pre></div>1188</div>1189<p>Accepts a wide range of Python callables, from plain functions and classes to1190<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>1191<p>If some of the annotations are strings (e.g., because1192<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> was used), <code class="xref py py-func docutils literal notranslate"><span class="pre">signature()</span></code> will1193attempt to automatically un-stringize the annotations using1194<a class="reference internal" href="annotationlib.html#annotationlib.get_annotations" title="annotationlib.get_annotations"><code class="xref py py-func docutils literal notranslate"><span class="pre">annotationlib.get_annotations()</span></code></a>. The1195<em>globals</em>, <em>locals</em>, and <em>eval_str</em> parameters are passed1196into <code class="xref py py-func docutils literal notranslate"><span class="pre">annotationlib.get_annotations()</span></code> when resolving the1197annotations; see the documentation for <code class="xref py py-func docutils literal notranslate"><span class="pre">annotationlib.get_annotations()</span></code>1198for instructions on how to use these parameters. A member of the1199<a class="reference internal" href="annotationlib.html#annotationlib.Format" title="annotationlib.Format"><code class="xref py py-class docutils literal notranslate"><span class="pre">annotationlib.Format</span></code></a> enum can be passed to the1200<em>annotation_format</em> parameter to control the format of the returned