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="types — Dynamic type creation and names for built-in types" />8<meta property="og:type" content="website" />9<meta property="og:url" content="https://docs.python.org/3/library/types.html" />10<meta property="og:site_name" content="Python documentation" />11<meta property="og:description" content="Source code: Lib/types.py This module defines utility functions to assist in dynamic creation of new types. It also defines names for some object types that are used by the standard Python interpre..." />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_types_8f69e28f.png" />15<meta property="og:image:alt" content="Source code: Lib/types.py This module defines utility functions to assist in dynamic creation of new types. It also defines names for some object types that are used by the standard Python interpre..." />16<meta name="description" content="Source code: Lib/types.py This module defines utility functions to assist in dynamic creation of new types. It also defines names for some object types that are used by the standard Python interpre..." />17<meta name="twitter:card" content="summary_large_image" />18<meta name="theme-color" content="#3776ab">19 20 <title>types — Dynamic type creation and names for built-in types — 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="copy — Shallow and deep copy operations" href="copy.html" />43 <link rel="prev" title="weakref — Weak references" href="weakref.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/types.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">types</span></code> — Dynamic type creation and names for built-in types</a><ul>108<li><a class="reference internal" href="#dynamic-type-creation">Dynamic Type Creation</a></li>109<li><a class="reference internal" href="#standard-interpreter-types">Standard Interpreter Types</a></li>110<li><a class="reference internal" href="#additional-utility-classes-and-functions">Additional Utility Classes and Functions</a></li>111<li><a class="reference internal" href="#coroutine-utility-functions">Coroutine Utility Functions</a></li>112</ul>113</li>114</ul>115 116 </div>117 <div>118 <h4>Previous topic</h4>119 <p class="topless"><a href="weakref.html"120 title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">weakref</span></code> — Weak references</a></p>121 </div>122 <div>123 <h4>Next topic</h4>124 <p class="topless"><a href="copy.html"125 title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">copy</span></code> — Shallow and deep copy operations</a></p>126 </div>127 <script>128 document.addEventListener('DOMContentLoaded', () => {129 const title = document.querySelector('meta[property="og:title"]').content;130 const elements = document.querySelectorAll('.improvepage');131 const pageurl = window.location.href.split('?')[0];132 elements.forEach(element => {133 const url = new URL(element.href.split('?')[0].replace("-nojs", ""));134 url.searchParams.set('pagetitle', title);135 url.searchParams.set('pageurl', pageurl);136 url.searchParams.set('pagesource', "library/types.rst");137 element.href = url.toString();138 });139 });140 </script>141 <div role="note" aria-label="source link">142 <h3>This page</h3>143 <ul class="this-page-menu">144 <li><a href="../bugs.html">Report a bug</a></li>145 <li><a class="improvepage" href="../improve-page-nojs.html">Improve this page</a></li>146 <li>147 <a href="https://github.com/python/cpython/blob/main/Doc/library/types.rst?plain=1"148 rel="nofollow">Show source149 </a>150 </li>151 152 </ul>153 </div>154 </nav>155 </div>156</div>157 158 159 <div class="related" role="navigation" aria-label="Related">160 <h3>Navigation</h3>161 <ul>162 <li class="right" style="margin-right: 10px">163 <a href="../genindex.html" title="General Index"164 accesskey="I">index</a></li>165 <li class="right" >166 <a href="../py-modindex.html" title="Python Module Index"167 >modules</a> |</li>168 <li class="right" >169 <a href="copy.html" title="copy — Shallow and deep copy operations"170 accesskey="N">next</a> |</li>171 <li class="right" >172 <a href="weakref.html" title="weakref — Weak references"173 accesskey="P">previous</a> |</li>174 175 <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"></li>176 <li><a href="https://www.python.org/">Python</a> »</li>177 <li class="switchers">178 <div class="language_switcher_placeholder"></div>179 <div class="version_switcher_placeholder"></div>180 </li>181 <li>182 183 </li>184 <li id="cpython-language-and-version">185 <a href="../index.html">3.15.0a6 Documentation</a> »186 </li>187 188 <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>189 <li class="nav-item nav-item-2"><a href="datatypes.html" accesskey="U">Data Types</a> »</li>190 <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">types</span></code> — Dynamic type creation and names for built-in types</a></li>191 <li class="right">192 193 194 <div class="inline-search" role="search">195 <form class="inline-search" action="../search.html" method="get">196 <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box">197 <input type="submit" value="Go">198 </form>199 </div>200 |201 </li>202 <li class="right">203<label class="theme-selector-label">204 Theme205 <select class="theme-selector" oninput="activateTheme(this.value)">206 <option value="auto" selected>Auto</option>207 <option value="light">Light</option>208 <option value="dark">Dark</option>209 </select>210</label> |</li>211 212 </ul>213 </div> 214 215 <div class="document">216 <div class="documentwrapper">217 <div class="bodywrapper">218 <div class="body" role="main">219 220 <section id="module-types">221<span id="types-dynamic-type-creation-and-names-for-built-in-types"></span><h1><code class="xref py py-mod docutils literal notranslate"><span class="pre">types</span></code> — Dynamic type creation and names for built-in types<a class="headerlink" href="#module-types" title="Link to this heading">¶</a></h1>222<p><strong>Source code:</strong> <a class="extlink-source reference external" href="https://github.com/python/cpython/tree/main/Lib/types.py">Lib/types.py</a></p>223<hr class="docutils" />224<p>This module defines utility functions to assist in dynamic creation of225new types.</p>226<p>It also defines names for some object types that are used by the standard227Python interpreter, but not exposed as builtins like <a class="reference internal" href="functions.html#int" title="int"><code class="xref py py-class docutils literal notranslate"><span class="pre">int</span></code></a> or228<a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> are.</p>229<p>Finally, it provides some additional type-related utility classes and functions230that are not fundamental enough to be builtins.</p>231<section id="dynamic-type-creation">232<h2>Dynamic Type Creation<a class="headerlink" href="#dynamic-type-creation" title="Link to this heading">¶</a></h2>233<dl class="py function">234<dt class="sig sig-object py" id="types.new_class">235<span class="sig-prename descclassname"><span class="pre">types.</span></span><span class="sig-name descname"><span class="pre">new_class</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">bases</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">()</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">kwds</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">exec_body</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="#types.new_class" title="Link to this definition">¶</a></dt>236<dd><p>Creates a class object dynamically using the appropriate metaclass.</p>237<p>The first three arguments are the components that make up a class238definition header: the class name, the base classes (in order), the239keyword arguments (such as <code class="docutils literal notranslate"><span class="pre">metaclass</span></code>).</p>240<p>The <em>exec_body</em> argument is a callback that is used to populate the241freshly created class namespace. It should accept the class namespace242as its sole argument and update the namespace directly with the class243contents. If no callback is provided, it has the same effect as passing244in <code class="docutils literal notranslate"><span class="pre">lambda</span> <span class="pre">ns:</span> <span class="pre">None</span></code>.</p>245<div class="versionadded">246<p><span class="versionmodified added">Added in version 3.3.</span></p>247</div>248</dd></dl>249 250<dl class="py function">251<dt class="sig sig-object py" id="types.prepare_class">252<span class="sig-prename descclassname"><span class="pre">types.</span></span><span class="sig-name descname"><span class="pre">prepare_class</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">bases</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">()</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">kwds</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="#types.prepare_class" title="Link to this definition">¶</a></dt>253<dd><p>Calculates the appropriate metaclass and creates the class namespace.</p>254<p>The arguments are the components that make up a class definition header:255the class name, the base classes (in order) and the keyword arguments256(such as <code class="docutils literal notranslate"><span class="pre">metaclass</span></code>).</p>257<p>The return value is a 3-tuple: <code class="docutils literal notranslate"><span class="pre">metaclass,</span> <span class="pre">namespace,</span> <span class="pre">kwds</span></code></p>258<p><em>metaclass</em> is the appropriate metaclass, <em>namespace</em> is the259prepared class namespace and <em>kwds</em> is an updated copy of the passed260in <em>kwds</em> argument with any <code class="docutils literal notranslate"><span class="pre">'metaclass'</span></code> entry removed. If no <em>kwds</em>261argument is passed in, this will be an empty dict.</p>262<div class="versionadded">263<p><span class="versionmodified added">Added in version 3.3.</span></p>264</div>265<div class="versionchanged">266<p><span class="versionmodified changed">Changed in version 3.6: </span>The default value for the <code class="docutils literal notranslate"><span class="pre">namespace</span></code> element of the returned267tuple has changed. Now an insertion-order-preserving mapping is268used when the metaclass does not have a <code class="docutils literal notranslate"><span class="pre">__prepare__</span></code> method.</p>269</div>270</dd></dl>271 272<div class="admonition seealso">273<p class="admonition-title">See also</p>274<dl class="simple">275<dt><a class="reference internal" href="../reference/datamodel.html#metaclasses"><span class="std std-ref">Metaclasses</span></a></dt><dd><p>Full details of the class creation process supported by these functions</p>276</dd>277<dt><span class="target" id="index-0"></span><a class="pep reference external" href="https://peps.python.org/pep-3115/"><strong>PEP 3115</strong></a> - Metaclasses in Python 3000</dt><dd><p>Introduced the <code class="docutils literal notranslate"><span class="pre">__prepare__</span></code> namespace hook</p>278</dd>279</dl>280</div>281<dl class="py function">282<dt class="sig sig-object py" id="types.resolve_bases">283<span class="sig-prename descclassname"><span class="pre">types.</span></span><span class="sig-name descname"><span class="pre">resolve_bases</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">bases</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#types.resolve_bases" title="Link to this definition">¶</a></dt>284<dd><p>Resolve MRO entries dynamically as specified by <span class="target" id="index-1"></span><a class="pep reference external" href="https://peps.python.org/pep-0560/"><strong>PEP 560</strong></a>.</p>285<p>This function looks for items in <em>bases</em> that are not instances of286<a class="reference internal" href="functions.html#type" title="type"><code class="xref py py-class docutils literal notranslate"><span class="pre">type</span></code></a>, and returns a tuple where each such object that has287an <a class="reference internal" href="../reference/datamodel.html#object.__mro_entries__" title="object.__mro_entries__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__mro_entries__()</span></code></a> method is replaced with an unpacked result of288calling this method. If a <em>bases</em> item is an instance of <code class="xref py py-class docutils literal notranslate"><span class="pre">type</span></code>,289or it doesn’t have an <code class="xref py py-meth docutils literal notranslate"><span class="pre">__mro_entries__()</span></code> method, then it is included in290the return tuple unchanged.</p>291<div class="versionadded">292<p><span class="versionmodified added">Added in version 3.7.</span></p>293</div>294</dd></dl>295 296<dl class="py function">297<dt class="sig sig-object py" id="types.get_original_bases">298<span class="sig-prename descclassname"><span class="pre">types.</span></span><span class="sig-name descname"><span class="pre">get_original_bases</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">cls</span></span></em>, <em class="sig-param"><span class="positional-only-separator o"><abbr title="Positional-only parameter separator (PEP 570)"><span class="pre">/</span></abbr></span></em><span class="sig-paren">)</span><a class="headerlink" href="#types.get_original_bases" title="Link to this definition">¶</a></dt>299<dd><p>Return the tuple of objects originally given as the bases of <em>cls</em> before300the <a class="reference internal" href="../reference/datamodel.html#object.__mro_entries__" title="object.__mro_entries__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__mro_entries__()</span></code></a> method has been called on any bases301(following the mechanisms laid out in <span class="target" id="index-2"></span><a class="pep reference external" href="https://peps.python.org/pep-0560/"><strong>PEP 560</strong></a>). This is useful for302introspecting <a class="reference internal" href="typing.html#user-defined-generics"><span class="std std-ref">Generics</span></a>.</p>303<p>For classes that have an <code class="docutils literal notranslate"><span class="pre">__orig_bases__</span></code> attribute, this304function returns the value of <code class="docutils literal notranslate"><span class="pre">cls.__orig_bases__</span></code>.305For classes without the <code class="docutils literal notranslate"><span class="pre">__orig_bases__</span></code> attribute,306<a class="reference internal" href="../reference/datamodel.html#type.__bases__" title="type.__bases__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">cls.__bases__</span></code></a> is returned.</p>307<p>Examples:</p>308<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span><span class="w"> </span><span class="nn">typing</span><span class="w"> </span><span class="kn">import</span> <span class="n">TypeVar</span><span class="p">,</span> <span class="n">Generic</span><span class="p">,</span> <span class="n">NamedTuple</span><span class="p">,</span> <span class="n">TypedDict</span>309 310<span class="n">T</span> <span class="o">=</span> <span class="n">TypeVar</span><span class="p">(</span><span class="s2">"T"</span><span class="p">)</span>311<span class="k">class</span><span class="w"> </span><span class="nc">Foo</span><span class="p">(</span><span class="n">Generic</span><span class="p">[</span><span class="n">T</span><span class="p">]):</span> <span class="o">...</span>312<span class="k">class</span><span class="w"> </span><span class="nc">Bar</span><span class="p">(</span><span class="n">Foo</span><span class="p">[</span><span class="nb">int</span><span class="p">],</span> <span class="nb">float</span><span class="p">):</span> <span class="o">...</span>313<span class="k">class</span><span class="w"> </span><span class="nc">Baz</span><span class="p">(</span><span class="nb">list</span><span class="p">[</span><span class="nb">str</span><span class="p">]):</span> <span class="o">...</span>314<span class="n">Eggs</span> <span class="o">=</span> <span class="n">NamedTuple</span><span class="p">(</span><span class="s2">"Eggs"</span><span class="p">,</span> <span class="p">[(</span><span class="s2">"a"</span><span class="p">,</span> <span class="nb">int</span><span class="p">),</span> <span class="p">(</span><span class="s2">"b"</span><span class="p">,</span> <span class="nb">str</span><span class="p">)])</span>315<span class="n">Spam</span> <span class="o">=</span> <span class="n">TypedDict</span><span class="p">(</span><span class="s2">"Spam"</span><span class="p">,</span> <span class="p">{</span><span class="s2">"a"</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="s2">"b"</span><span class="p">:</span> <span class="nb">str</span><span class="p">})</span>316 317<span class="k">assert</span> <span class="n">Bar</span><span class="o">.</span><span class="vm">__bases__</span> <span class="o">==</span> <span class="p">(</span><span class="n">Foo</span><span class="p">,</span> <span class="nb">float</span><span class="p">)</span>318<span class="k">assert</span> <span class="n">get_original_bases</span><span class="p">(</span><span class="n">Bar</span><span class="p">)</span> <span class="o">==</span> <span class="p">(</span><span class="n">Foo</span><span class="p">[</span><span class="nb">int</span><span class="p">],</span> <span class="nb">float</span><span class="p">)</span>319 320<span class="k">assert</span> <span class="n">Baz</span><span class="o">.</span><span class="vm">__bases__</span> <span class="o">==</span> <span class="p">(</span><span class="nb">list</span><span class="p">,)</span>321<span class="k">assert</span> <span class="n">get_original_bases</span><span class="p">(</span><span class="n">Baz</span><span class="p">)</span> <span class="o">==</span> <span class="p">(</span><span class="nb">list</span><span class="p">[</span><span class="nb">str</span><span class="p">],)</span>322 323<span class="k">assert</span> <span class="n">Eggs</span><span class="o">.</span><span class="vm">__bases__</span> <span class="o">==</span> <span class="p">(</span><span class="nb">tuple</span><span class="p">,)</span>324<span class="k">assert</span> <span class="n">get_original_bases</span><span class="p">(</span><span class="n">Eggs</span><span class="p">)</span> <span class="o">==</span> <span class="p">(</span><span class="n">NamedTuple</span><span class="p">,)</span>325 326<span class="k">assert</span> <span class="n">Spam</span><span class="o">.</span><span class="vm">__bases__</span> <span class="o">==</span> <span class="p">(</span><span class="nb">dict</span><span class="p">,)</span>327<span class="k">assert</span> <span class="n">get_original_bases</span><span class="p">(</span><span class="n">Spam</span><span class="p">)</span> <span class="o">==</span> <span class="p">(</span><span class="n">TypedDict</span><span class="p">,)</span>328 329<span class="k">assert</span> <span class="nb">int</span><span class="o">.</span><span class="vm">__bases__</span> <span class="o">==</span> <span class="p">(</span><span class="nb">object</span><span class="p">,)</span>330<span class="k">assert</span> <span class="n">get_original_bases</span><span class="p">(</span><span class="nb">int</span><span class="p">)</span> <span class="o">==</span> <span class="p">(</span><span class="nb">object</span><span class="p">,)</span>331</pre></div>332</div>333<div class="versionadded">334<p><span class="versionmodified added">Added in version 3.12.</span></p>335</div>336</dd></dl>337 338<div class="admonition seealso">339<p class="admonition-title">See also</p>340<p><span class="target" id="index-3"></span><a class="pep reference external" href="https://peps.python.org/pep-0560/"><strong>PEP 560</strong></a> - Core support for typing module and generic types</p>341</div>342</section>343<section id="standard-interpreter-types">344<h2>Standard Interpreter Types<a class="headerlink" href="#standard-interpreter-types" title="Link to this heading">¶</a></h2>345<p>This module provides names for many of the types that are required to346implement a Python interpreter. It deliberately avoids including some of347the types that arise only incidentally during processing such as the348<code class="docutils literal notranslate"><span class="pre">listiterator</span></code> type.</p>349<p>Typical use of these names is for <a class="reference internal" href="functions.html#isinstance" title="isinstance"><code class="xref py py-func docutils literal notranslate"><span class="pre">isinstance()</span></code></a> or350<a class="reference internal" href="functions.html#issubclass" title="issubclass"><code class="xref py py-func docutils literal notranslate"><span class="pre">issubclass()</span></code></a> checks.</p>351<p>If you instantiate any of these types, note that signatures may vary between Python versions.</p>352<p>Standard names are defined for the following types:</p>353<dl class="py data">354<dt class="sig sig-object py" id="types.NoneType">355<span class="sig-prename descclassname"><span class="pre">types.</span></span><span class="sig-name descname"><span class="pre">NoneType</span></span><a class="headerlink" href="#types.NoneType" title="Link to this definition">¶</a></dt>356<dd><p>The type of <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-data docutils literal notranslate"><span class="pre">None</span></code></a>.</p>357<div class="versionadded">358<p><span class="versionmodified added">Added in version 3.10.</span></p>359</div>360</dd></dl>361 362<dl class="py data">363<dt class="sig sig-object py" id="types.FunctionType">364<span class="sig-prename descclassname"><span class="pre">types.</span></span><span class="sig-name descname"><span class="pre">FunctionType</span></span><a class="headerlink" href="#types.FunctionType" title="Link to this definition">¶</a></dt>365<dt class="sig sig-object py" id="types.LambdaType">366<span class="sig-prename descclassname"><span class="pre">types.</span></span><span class="sig-name descname"><span class="pre">LambdaType</span></span><a class="headerlink" href="#types.LambdaType" title="Link to this definition">¶</a></dt>367<dd><p>The type of user-defined functions and functions created by368<a class="reference internal" href="../reference/expressions.html#lambda"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">lambda</span></code></a> expressions.</p>369<p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">function.__new__</span></code> with argument <code class="docutils literal notranslate"><span class="pre">code</span></code>.</p>370<p>The audit event only occurs for direct instantiation of function objects,371and is not raised for normal compilation.</p>372</dd></dl>373 374<dl class="py data">375<dt class="sig sig-object py" id="types.GeneratorType">376<span class="sig-prename descclassname"><span class="pre">types.</span></span><span class="sig-name descname"><span class="pre">GeneratorType</span></span><a class="headerlink" href="#types.GeneratorType" title="Link to this definition">¶</a></dt>377<dd><p>The type of <a class="reference internal" href="../glossary.html#term-generator"><span class="xref std std-term">generator</span></a>-iterator objects, created by378generator functions.</p>379</dd></dl>380 381<dl class="py data">382<dt class="sig sig-object py" id="types.CoroutineType">383<span class="sig-prename descclassname"><span class="pre">types.</span></span><span class="sig-name descname"><span class="pre">CoroutineType</span></span><a class="headerlink" href="#types.CoroutineType" title="Link to this definition">¶</a></dt>384<dd><p>The type of <a class="reference internal" href="../glossary.html#term-coroutine"><span class="xref std std-term">coroutine</span></a> objects, created by385<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>386<div class="versionadded">387<p><span class="versionmodified added">Added in version 3.5.</span></p>388</div>389</dd></dl>390 391<dl class="py data">392<dt class="sig sig-object py" id="types.AsyncGeneratorType">393<span class="sig-prename descclassname"><span class="pre">types.</span></span><span class="sig-name descname"><span class="pre">AsyncGeneratorType</span></span><a class="headerlink" href="#types.AsyncGeneratorType" title="Link to this definition">¶</a></dt>394<dd><p>The type of <a class="reference internal" href="../glossary.html#term-asynchronous-generator"><span class="xref std std-term">asynchronous generator</span></a>-iterator objects, created by395asynchronous generator functions.</p>396<div class="versionadded">397<p><span class="versionmodified added">Added in version 3.6.</span></p>398</div>399</dd></dl>400 401<dl class="py class">402<dt class="sig sig-object py" id="types.CodeType">403<em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">types.</span></span><span class="sig-name descname"><span class="pre">CodeType</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">kwargs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#types.CodeType" title="Link to this definition">¶</a></dt>404<dd><p id="index-4">The type of <a class="reference internal" href="../reference/datamodel.html#code-objects"><span class="std std-ref">code objects</span></a> such as returned by <a class="reference internal" href="functions.html#compile" title="compile"><code class="xref py py-func docutils literal notranslate"><span class="pre">compile()</span></code></a>.</p>405<p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">code.__new__</span></code> with arguments <code class="docutils literal notranslate"><span class="pre">code</span></code>, <code class="docutils literal notranslate"><span class="pre">filename</span></code>, <code class="docutils literal notranslate"><span class="pre">name</span></code>, <code class="docutils literal notranslate"><span class="pre">argcount</span></code>, <code class="docutils literal notranslate"><span class="pre">posonlyargcount</span></code>, <code class="docutils literal notranslate"><span class="pre">kwonlyargcount</span></code>, <code class="docutils literal notranslate"><span class="pre">nlocals</span></code>, <code class="docutils literal notranslate"><span class="pre">stacksize</span></code>, <code class="docutils literal notranslate"><span class="pre">flags</span></code>.</p>406<p>Note that the audited arguments may not match the names or positions407required by the initializer. The audit event only occurs for direct408instantiation of code objects, and is not raised for normal compilation.</p>409</dd></dl>410 411<dl class="py data">412<dt class="sig sig-object py" id="types.CellType">413<span class="sig-prename descclassname"><span class="pre">types.</span></span><span class="sig-name descname"><span class="pre">CellType</span></span><a class="headerlink" href="#types.CellType" title="Link to this definition">¶</a></dt>414<dd><p>The type for cell objects: such objects are used as containers for415a function’s <a class="reference internal" href="../glossary.html#term-closure-variable"><span class="xref std std-term">closure variables</span></a>.</p>416<div class="versionadded">417<p><span class="versionmodified added">Added in version 3.8.</span></p>418</div>419</dd></dl>420 421<dl class="py data">422<dt class="sig sig-object py" id="types.MethodType">423<span class="sig-prename descclassname"><span class="pre">types.</span></span><span class="sig-name descname"><span class="pre">MethodType</span></span><a class="headerlink" href="#types.MethodType" title="Link to this definition">¶</a></dt>424<dd><p>The type of methods of user-defined class instances.</p>425</dd></dl>426 427<dl class="py data">428<dt class="sig sig-object py" id="types.BuiltinFunctionType">429<span class="sig-prename descclassname"><span class="pre">types.</span></span><span class="sig-name descname"><span class="pre">BuiltinFunctionType</span></span><a class="headerlink" href="#types.BuiltinFunctionType" title="Link to this definition">¶</a></dt>430<dt class="sig sig-object py" id="types.BuiltinMethodType">431<span class="sig-prename descclassname"><span class="pre">types.</span></span><span class="sig-name descname"><span class="pre">BuiltinMethodType</span></span><a class="headerlink" href="#types.BuiltinMethodType" title="Link to this definition">¶</a></dt>432<dd><p>The type of built-in functions like <a class="reference internal" href="functions.html#len" title="len"><code class="xref py py-func docutils literal notranslate"><span class="pre">len()</span></code></a> or <a class="reference internal" href="sys.html#sys.exit" title="sys.exit"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.exit()</span></code></a>, and433methods of built-in classes. (Here, the term “built-in” means “written in434C”.)</p>435</dd></dl>436 437<dl class="py data">438<dt class="sig sig-object py" id="types.WrapperDescriptorType">439<span class="sig-prename descclassname"><span class="pre">types.</span></span><span class="sig-name descname"><span class="pre">WrapperDescriptorType</span></span><a class="headerlink" href="#types.WrapperDescriptorType" title="Link to this definition">¶</a></dt>440<dd><p>The type of methods of some built-in data types and base classes such as441<a class="reference internal" href="../reference/datamodel.html#object.__init__" title="object.__init__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">object.__init__()</span></code></a> or <a class="reference internal" href="../reference/datamodel.html#object.__lt__" title="object.__lt__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">object.__lt__()</span></code></a>.</p>442<div class="versionadded">443<p><span class="versionmodified added">Added in version 3.7.</span></p>444</div>445</dd></dl>446 447<dl class="py data">448<dt class="sig sig-object py" id="types.MethodWrapperType">449<span class="sig-prename descclassname"><span class="pre">types.</span></span><span class="sig-name descname"><span class="pre">MethodWrapperType</span></span><a class="headerlink" href="#types.MethodWrapperType" title="Link to this definition">¶</a></dt>450<dd><p>The type of <em>bound</em> methods of some built-in data types and base classes.451For example it is the type of <code class="code docutils literal notranslate"><span class="pre">object().__str__</span></code>.</p>452<div class="versionadded">453<p><span class="versionmodified added">Added in version 3.7.</span></p>454</div>455</dd></dl>456 457<dl class="py data">458<dt class="sig sig-object py" id="types.NotImplementedType">459<span class="sig-prename descclassname"><span class="pre">types.</span></span><span class="sig-name descname"><span class="pre">NotImplementedType</span></span><a class="headerlink" href="#types.NotImplementedType" title="Link to this definition">¶</a></dt>460<dd><p>The type of <a class="reference internal" href="constants.html#NotImplemented" title="NotImplemented"><code class="xref py py-data docutils literal notranslate"><span class="pre">NotImplemented</span></code></a>.</p>461<div class="versionadded">462<p><span class="versionmodified added">Added in version 3.10.</span></p>463</div>464</dd></dl>465 466<dl class="py data">467<dt class="sig sig-object py" id="types.MethodDescriptorType">468<span class="sig-prename descclassname"><span class="pre">types.</span></span><span class="sig-name descname"><span class="pre">MethodDescriptorType</span></span><a class="headerlink" href="#types.MethodDescriptorType" title="Link to this definition">¶</a></dt>469<dd><p>The type of methods of some built-in data types such as <a class="reference internal" href="stdtypes.html#str.join" title="str.join"><code class="xref py py-meth docutils literal notranslate"><span class="pre">str.join()</span></code></a>.</p>470<div class="versionadded">471<p><span class="versionmodified added">Added in version 3.7.</span></p>472</div>473</dd></dl>474 475<dl class="py data">476<dt class="sig sig-object py" id="types.ClassMethodDescriptorType">477<span class="sig-prename descclassname"><span class="pre">types.</span></span><span class="sig-name descname"><span class="pre">ClassMethodDescriptorType</span></span><a class="headerlink" href="#types.ClassMethodDescriptorType" title="Link to this definition">¶</a></dt>478<dd><p>The type of <em>unbound</em> class methods of some built-in data types such as479<code class="docutils literal notranslate"><span class="pre">dict.__dict__['fromkeys']</span></code>.</p>480<div class="versionadded">481<p><span class="versionmodified added">Added in version 3.7.</span></p>482</div>483</dd></dl>484 485<dl class="py class">486<dt class="sig sig-object py" id="types.ModuleType">487<em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">types.</span></span><span class="sig-name descname"><span class="pre">ModuleType</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">doc</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="#types.ModuleType" title="Link to this definition">¶</a></dt>488<dd><p>The type of <a class="reference internal" href="../glossary.html#term-module"><span class="xref std std-term">modules</span></a>. The constructor takes the name of the489module to be created and optionally its <a class="reference internal" href="../glossary.html#term-docstring"><span class="xref std std-term">docstring</span></a>.</p>490<div class="admonition seealso">491<p class="admonition-title">See also</p>492<dl class="simple">493<dt><a class="reference internal" href="../reference/datamodel.html#module-objects"><span class="std std-ref">Documentation on module objects</span></a></dt><dd><p>Provides details on the special attributes that can be found on494instances of <code class="xref py py-class docutils literal notranslate"><span class="pre">ModuleType</span></code>.</p>495</dd>496<dt><a class="reference internal" href="importlib.html#importlib.util.module_from_spec" title="importlib.util.module_from_spec"><code class="xref py py-func docutils literal notranslate"><span class="pre">importlib.util.module_from_spec()</span></code></a></dt><dd><p>Modules created using the <code class="xref py py-class docutils literal notranslate"><span class="pre">ModuleType</span></code> constructor are497created with many of their special attributes unset or set to default498values. <code class="xref py py-func docutils literal notranslate"><span class="pre">module_from_spec()</span></code> provides a more robust way of499creating <code class="xref py py-class docutils literal notranslate"><span class="pre">ModuleType</span></code> instances which ensures the various500attributes are set appropriately.</p>501</dd>502</dl>503</div>504</dd></dl>505 506<dl class="py data">507<dt class="sig sig-object py" id="types.EllipsisType">508<span class="sig-prename descclassname"><span class="pre">types.</span></span><span class="sig-name descname"><span class="pre">EllipsisType</span></span><a class="headerlink" href="#types.EllipsisType" title="Link to this definition">¶</a></dt>509<dd><p>The type of <a class="reference internal" href="constants.html#Ellipsis" title="Ellipsis"><code class="xref py py-data docutils literal notranslate"><span class="pre">Ellipsis</span></code></a>.</p>510<div class="versionadded">511<p><span class="versionmodified added">Added in version 3.10.</span></p>512</div>513</dd></dl>514 515<dl class="py class">516<dt class="sig sig-object py" id="types.GenericAlias">517<em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">types.</span></span><span class="sig-name descname"><span class="pre">GenericAlias</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">t_origin</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">t_args</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#types.GenericAlias" title="Link to this definition">¶</a></dt>518<dd><p>The type of <a class="reference internal" href="stdtypes.html#types-genericalias"><span class="std std-ref">parameterized generics</span></a> such as519<code class="docutils literal notranslate"><span class="pre">list[int]</span></code>.</p>520<p><code class="docutils literal notranslate"><span class="pre">t_origin</span></code> should be a non-parameterized generic class, such as <code class="docutils literal notranslate"><span class="pre">list</span></code>,521<code class="docutils literal notranslate"><span class="pre">tuple</span></code> or <code class="docutils literal notranslate"><span class="pre">dict</span></code>. <code class="docutils literal notranslate"><span class="pre">t_args</span></code> should be 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> (possibly of522length 1) of types which parameterize <code class="docutils literal notranslate"><span class="pre">t_origin</span></code>:</p>523<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span><span class="w"> </span><span class="nn">types</span><span class="w"> </span><span class="kn">import</span> <span class="n">GenericAlias</span>524 525<span class="gp">>>> </span><span class="nb">list</span><span class="p">[</span><span class="nb">int</span><span class="p">]</span> <span class="o">==</span> <span class="n">GenericAlias</span><span class="p">(</span><span class="nb">list</span><span class="p">,</span> <span class="p">(</span><span class="nb">int</span><span class="p">,))</span>526<span class="go">True</span>527<span class="gp">>>> </span><span class="nb">dict</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="nb">int</span><span class="p">]</span> <span class="o">==</span> <span class="n">GenericAlias</span><span class="p">(</span><span class="nb">dict</span><span class="p">,</span> <span class="p">(</span><span class="nb">str</span><span class="p">,</span> <span class="nb">int</span><span class="p">))</span>528<span class="go">True</span>529</pre></div>530</div>531<div class="versionadded">532<p><span class="versionmodified added">Added in version 3.9.</span></p>533</div>534<div class="versionchanged">535<p><span class="versionmodified changed">Changed in version 3.9.2: </span>This type can now be subclassed.</p>536</div>537<div class="admonition seealso">538<p class="admonition-title">See also</p>539<dl class="simple">540<dt><a class="reference internal" href="stdtypes.html#types-genericalias"><span class="std std-ref">Generic Alias Types</span></a></dt><dd><p>In-depth documentation on instances of <code class="xref py py-class docutils literal notranslate"><span class="pre">types.GenericAlias</span></code></p>541</dd>542<dt><span class="target" id="index-5"></span><a class="pep reference external" href="https://peps.python.org/pep-0585/"><strong>PEP 585</strong></a> - Type Hinting Generics In Standard Collections</dt><dd><p>Introducing the <code class="xref py py-class docutils literal notranslate"><span class="pre">types.GenericAlias</span></code> class</p>543</dd>544</dl>545</div>546</dd></dl>547 548<dl class="py class">549<dt class="sig sig-object py" id="types.UnionType">550<em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">types.</span></span><span class="sig-name descname"><span class="pre">UnionType</span></span><a class="headerlink" href="#types.UnionType" title="Link to this definition">¶</a></dt>551<dd><p>The type of <a class="reference internal" href="stdtypes.html#types-union"><span class="std std-ref">union type expressions</span></a>.</p>552<div class="versionadded">553<p><span class="versionmodified added">Added in version 3.10.</span></p>554</div>555<div class="versionchanged">556<p><span class="versionmodified changed">Changed in version 3.14: </span>This is now an alias for <a class="reference internal" href="typing.html#typing.Union" title="typing.Union"><code class="xref py py-class docutils literal notranslate"><span class="pre">typing.Union</span></code></a>.</p>557</div>558</dd></dl>559 560<dl class="py class">561<dt class="sig sig-object py" id="types.TracebackType">562<em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">types.</span></span><span class="sig-name descname"><span class="pre">TracebackType</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">tb_next</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">tb_frame</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">tb_lasti</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">tb_lineno</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#types.TracebackType" title="Link to this definition">¶</a></dt>563<dd><p>The type of traceback objects such as found in <code class="docutils literal notranslate"><span class="pre">sys.exception().__traceback__</span></code>.</p>564<p>See <a class="reference internal" href="../reference/datamodel.html#traceback-objects"><span class="std std-ref">the language reference</span></a> for details of the565available attributes and operations, and guidance on creating tracebacks566dynamically.</p>567</dd></dl>568 569<dl class="py data">570<dt class="sig sig-object py" id="types.FrameType">571<span class="sig-prename descclassname"><span class="pre">types.</span></span><span class="sig-name descname"><span class="pre">FrameType</span></span><a class="headerlink" href="#types.FrameType" title="Link to this definition">¶</a></dt>572<dd><p>The type of <a class="reference internal" href="../reference/datamodel.html#frame-objects"><span class="std std-ref">frame objects</span></a> such as found in573<a class="reference internal" href="../reference/datamodel.html#traceback.tb_frame" title="traceback.tb_frame"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tb.tb_frame</span></code></a> if <code class="docutils literal notranslate"><span class="pre">tb</span></code> is a traceback object.</p>574</dd></dl>575 576<dl class="py data">577<dt class="sig sig-object py" id="types.FrameLocalsProxyType">578<span class="sig-prename descclassname"><span class="pre">types.</span></span><span class="sig-name descname"><span class="pre">FrameLocalsProxyType</span></span><a class="headerlink" href="#types.FrameLocalsProxyType" title="Link to this definition">¶</a></dt>579<dd><p>The type of frame locals proxy objects, as found on the580<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">frame.f_locals</span></code></a> attribute.</p>581<div class="versionadded">582<p><span class="versionmodified added">Added in version 3.15.</span></p>583</div>584<div class="admonition seealso">585<p class="admonition-title">See also</p>586<p><span class="target" id="index-6"></span><a class="pep reference external" href="https://peps.python.org/pep-0667/"><strong>PEP 667</strong></a></p>587</div>588</dd></dl>589 590<dl class="py data">591<dt class="sig sig-object py" id="types.LazyImportType">592<span class="sig-prename descclassname"><span class="pre">types.</span></span><span class="sig-name descname"><span class="pre">LazyImportType</span></span><a class="headerlink" href="#types.LazyImportType" title="Link to this definition">¶</a></dt>593<dd><p>The type of lazy import proxy objects. These objects are created when a594module is lazily imported and serve as placeholders until the module is595actually accessed. This type can be used to detect lazy imports596programmatically.</p>597<div class="versionadded">598<p><span class="versionmodified added">Added in version 3.15.0a6 (unreleased).</span></p>599</div>600<div class="admonition seealso">601<p class="admonition-title">See also</p>602<p><span class="target" id="index-7"></span><a class="pep reference external" href="https://peps.python.org/pep-0810/"><strong>PEP 810</strong></a></p>603</div>604</dd></dl>605 606<dl class="py data">607<dt class="sig sig-object py" id="types.GetSetDescriptorType">608<span class="sig-prename descclassname"><span class="pre">types.</span></span><span class="sig-name descname"><span class="pre">GetSetDescriptorType</span></span><a class="headerlink" href="#types.GetSetDescriptorType" title="Link to this definition">¶</a></dt>609<dd><p>The type of objects defined in extension modules with <code class="docutils literal notranslate"><span class="pre">PyGetSetDef</span></code>, such610as <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">FrameType.f_locals</span></code></a> or <code class="docutils literal notranslate"><span class="pre">array.array.typecode</span></code>.611This type is used as612descriptor for object attributes; it has the same purpose as the613<a class="reference internal" href="functions.html#property" title="property"><code class="xref py py-class docutils literal notranslate"><span class="pre">property</span></code></a> type, but for classes defined in extension modules.</p>614</dd></dl>615 616<dl class="py data">617<dt class="sig sig-object py" id="types.MemberDescriptorType">618<span class="sig-prename descclassname"><span class="pre">types.</span></span><span class="sig-name descname"><span class="pre">MemberDescriptorType</span></span><a class="headerlink" href="#types.MemberDescriptorType" title="Link to this definition">¶</a></dt>619<dd><p>The type of objects defined in extension modules with <code class="docutils literal notranslate"><span class="pre">PyMemberDef</span></code>, such620as <code class="docutils literal notranslate"><span class="pre">datetime.timedelta.days</span></code>. This type is used as descriptor for simple C621data members which use standard conversion functions; it has the same purpose622as the <a class="reference internal" href="functions.html#property" title="property"><code class="xref py py-class docutils literal notranslate"><span class="pre">property</span></code></a> type, but for classes defined in extension modules.</p>623<p>In addition, when a class is defined with a <a class="reference internal" href="../reference/datamodel.html#object.__slots__" title="object.__slots__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__slots__</span></code></a> attribute, then for624each slot, an instance of <code class="xref py py-class docutils literal notranslate"><span class="pre">MemberDescriptorType</span></code> will be added as an attribute625on the class. This allows the slot to appear in the class’s <a class="reference internal" href="../reference/datamodel.html#type.__dict__" title="type.__dict__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__dict__</span></code></a>.</p>626<div class="impl-detail compound">627<p><strong>CPython implementation detail:</strong> In other implementations of Python, this type may be identical to628<code class="docutils literal notranslate"><span class="pre">GetSetDescriptorType</span></code>.</p>629</div>630</dd></dl>631 632<dl class="py class">633<dt class="sig sig-object py" id="types.MappingProxyType">634<em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">types.</span></span><span class="sig-name descname"><span class="pre">MappingProxyType</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">mapping</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#types.MappingProxyType" title="Link to this definition">¶</a></dt>635<dd><p>Read-only proxy of a mapping. It provides a dynamic view on the mapping’s636entries, which means that when the mapping changes, the view reflects these637changes.</p>638<div class="versionadded">639<p><span class="versionmodified added">Added in version 3.3.</span></p>640</div>641<div class="versionchanged">642<p><span class="versionmodified changed">Changed in version 3.9: </span>Updated to support the new union (<code class="docutils literal notranslate"><span class="pre">|</span></code>) operator from <span class="target" id="index-8"></span><a class="pep reference external" href="https://peps.python.org/pep-0584/"><strong>PEP 584</strong></a>, which643simply delegates to the underlying mapping.</p>644</div>645<dl class="describe">646<dt class="sig sig-object">647<span class="sig-name descname"><span class="pre">key</span> <span class="pre">in</span> <span class="pre">proxy</span></span></dt>648<dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the underlying mapping has a key <em>key</em>, else649<code class="docutils literal notranslate"><span class="pre">False</span></code>.</p>650</dd></dl>651 652<dl class="describe">653<dt class="sig sig-object">654<span class="sig-name descname"><span class="pre">proxy[key]</span></span></dt>655<dd><p>Return the item of the underlying mapping with key <em>key</em>. Raises a656<a class="reference internal" href="exceptions.html#KeyError" title="KeyError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">KeyError</span></code></a> if <em>key</em> is not in the underlying mapping.</p>657</dd></dl>658 659<dl class="describe">660<dt class="sig sig-object">661<span class="sig-name descname"><span class="pre">iter(proxy)</span></span></dt>662<dd><p>Return an iterator over the keys of the underlying mapping. This is a663shortcut for <code class="docutils literal notranslate"><span class="pre">iter(proxy.keys())</span></code>.</p>664</dd></dl>665 666<dl class="describe">667<dt class="sig sig-object">668<span class="sig-name descname"><span class="pre">len(proxy)</span></span></dt>669<dd><p>Return the number of items in the underlying mapping.</p>670</dd></dl>671 672<dl class="py method">673<dt class="sig sig-object py" id="types.MappingProxyType.copy">674<span class="sig-name descname"><span class="pre">copy</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#types.MappingProxyType.copy" title="Link to this definition">¶</a></dt>675<dd><p>Return a shallow copy of the underlying mapping.</p>676</dd></dl>677 678<dl class="py method">679<dt class="sig sig-object py" id="types.MappingProxyType.get">680<span class="sig-name descname"><span class="pre">get</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">key</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">default</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#types.MappingProxyType.get" title="Link to this definition">¶</a></dt>681<dd><p>Return the value for <em>key</em> if <em>key</em> is in the underlying mapping, else682<em>default</em>. If <em>default</em> is not given, it defaults to <code class="docutils literal notranslate"><span class="pre">None</span></code>, so that683this method never raises a <a class="reference internal" href="exceptions.html#KeyError" title="KeyError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">KeyError</span></code></a>.</p>684</dd></dl>685 686<dl class="py method">687<dt class="sig sig-object py" id="types.MappingProxyType.items">688<span class="sig-name descname"><span class="pre">items</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#types.MappingProxyType.items" title="Link to this definition">¶</a></dt>689<dd><p>Return a new view of the underlying mapping’s items (<code class="docutils literal notranslate"><span class="pre">(key,</span> <span class="pre">value)</span></code>690pairs).</p>691</dd></dl>692 693<dl class="py method">694<dt class="sig sig-object py" id="types.MappingProxyType.keys">695<span class="sig-name descname"><span class="pre">keys</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#types.MappingProxyType.keys" title="Link to this definition">¶</a></dt>696<dd><p>Return a new view of the underlying mapping’s keys.</p>697</dd></dl>698 699<dl class="py method">700<dt class="sig sig-object py" id="types.MappingProxyType.values">701<span class="sig-name descname"><span class="pre">values</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#types.MappingProxyType.values" title="Link to this definition">¶</a></dt>702<dd><p>Return a new view of the underlying mapping’s values.</p>703</dd></dl>704 705<dl class="describe">706<dt class="sig sig-object">707<span class="sig-name descname"><span class="pre">reversed(proxy)</span></span></dt>708<dd><p>Return a reverse iterator over the keys of the underlying mapping.</p>709<div class="versionadded">710<p><span class="versionmodified added">Added in version 3.9.</span></p>711</div>712</dd></dl>713 714<dl class="describe">715<dt class="sig sig-object">716<span class="sig-name descname"><span class="pre">hash(proxy)</span></span></dt>717<dd><p>Return a hash of the underlying mapping.</p>718<div class="versionadded">719<p><span class="versionmodified added">Added in version 3.12.</span></p>720</div>721</dd></dl>722 723</dd></dl>724 725<dl class="py class">726<dt class="sig sig-object py" id="types.CapsuleType">727<em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">types.</span></span><span class="sig-name descname"><span class="pre">CapsuleType</span></span><a class="headerlink" href="#types.CapsuleType" title="Link to this definition">¶</a></dt>728<dd><p>The type of <a class="reference internal" href="../c-api/capsule.html#capsules"><span class="std std-ref">capsule objects</span></a>.</p>729<div class="versionadded">730<p><span class="versionmodified added">Added in version 3.13.</span></p>731</div>732</dd></dl>733 734</section>735<section id="additional-utility-classes-and-functions">736<h2>Additional Utility Classes and Functions<a class="headerlink" href="#additional-utility-classes-and-functions" title="Link to this heading">¶</a></h2>737<dl class="py class">738<dt class="sig sig-object py" id="types.SimpleNamespace">739<em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">types.</span></span><span class="sig-name descname"><span class="pre">SimpleNamespace</span></span><a class="headerlink" href="#types.SimpleNamespace" title="Link to this definition">¶</a></dt>740<dd><p>A simple <a class="reference internal" href="functions.html#object" title="object"><code class="xref py py-class docutils literal notranslate"><span class="pre">object</span></code></a> subclass that provides attribute access to its741namespace, as well as a meaningful repr.</p>742<p>Unlike <a class="reference internal" href="functions.html#object" title="object"><code class="xref py py-class docutils literal notranslate"><span class="pre">object</span></code></a>, with <code class="xref py py-class docutils literal notranslate"><span class="pre">SimpleNamespace</span></code> you can add and remove743attributes.</p>744<p><code class="xref py py-class docutils literal notranslate"><span class="pre">SimpleNamespace</span></code> objects may be initialized745in the same way as <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>: either with keyword arguments,746with a single positional argument, or with both.747When initialized with keyword arguments,748those are directly added to the underlying namespace.749Alternatively, when initialized with a positional argument,750the underlying namespace will be updated with key-value pairs751from that argument (either a mapping object or752an <a class="reference internal" href="../glossary.html#term-iterable"><span class="xref std std-term">iterable</span></a> object producing key-value pairs).753All such keys must be strings.</p>754<p>The type is roughly equivalent to the following code:</p>755<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">class</span><span class="w"> </span><span class="nc">SimpleNamespace</span><span class="p">:</span>756 <span class="k">def</span><span class="w"> </span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">mapping_or_iterable</span><span class="o">=</span><span class="p">(),</span> <span class="o">/</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>757 <span class="bp">self</span><span class="o">.</span><span class="vm">__dict__</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">mapping_or_iterable</span><span class="p">)</span>758 <span class="bp">self</span><span class="o">.</span><span class="vm">__dict__</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">kwargs</span><span class="p">)</span>759 760 <span class="k">def</span><span class="w"> </span><span class="fm">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>761 <span class="n">items</span> <span class="o">=</span> <span class="p">(</span><span class="sa">f</span><span class="s2">"</span><span class="si">{</span><span class="n">k</span><span class="si">}</span><span class="s2">=</span><span class="si">{</span><span class="n">v</span><span class="si">!r}</span><span class="s2">"</span> <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__dict__</span><span class="o">.</span><span class="n">items</span><span class="p">())</span>762 <span class="k">return</span> <span class="s2">"</span><span class="si">{}</span><span class="s2">(</span><span class="si">{}</span><span class="s2">)"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">type</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span> <span class="s2">", "</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">items</span><span class="p">))</span>763 764 <span class="k">def</span><span class="w"> </span><span class="fm">__eq__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>765 <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">SimpleNamespace</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">other</span><span class="p">,</span> <span class="n">SimpleNamespace</span><span class="p">):</span>766 <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__dict__</span> <span class="o">==</span> <span class="n">other</span><span class="o">.</span><span class="vm">__dict__</span>767 <span class="k">return</span> <span class="bp">NotImplemented</span>768</pre></div>769</div>770<p><code class="docutils literal notranslate"><span class="pre">SimpleNamespace</span></code> may be useful as a replacement for <code class="docutils literal notranslate"><span class="pre">class</span> <span class="pre">NS:</span> <span class="pre">pass</span></code>.771However, for a structured record type use <a class="reference internal" href="collections.html#collections.namedtuple" title="collections.namedtuple"><code class="xref py py-func docutils literal notranslate"><span class="pre">namedtuple()</span></code></a>772instead.</p>773<p><code class="xref py py-class docutils literal notranslate"><span class="pre">SimpleNamespace</span></code> objects are supported by <a class="reference internal" href="copy.html#copy.replace" title="copy.replace"><code class="xref py py-func docutils literal notranslate"><span class="pre">copy.replace()</span></code></a>.</p>774<div class="versionadded">775<p><span class="versionmodified added">Added in version 3.3.</span></p>776</div>777<div class="versionchanged">778<p><span class="versionmodified changed">Changed in version 3.9: </span>Attribute order in the repr changed from alphabetical to insertion (like779<code class="docutils literal notranslate"><span class="pre">dict</span></code>).</p>780</div>781<div class="versionchanged">782<p><span class="versionmodified changed">Changed in version 3.13: </span>Added support for an optional positional argument.</p>783</div>784</dd></dl>785 786<dl class="py function">787<dt class="sig sig-object py" id="types.DynamicClassAttribute">788<span class="sig-prename descclassname"><span class="pre">types.</span></span><span class="sig-name descname"><span class="pre">DynamicClassAttribute</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fget</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">fset</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">fdel</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">doc</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="#types.DynamicClassAttribute" title="Link to this definition">¶</a></dt>789<dd><p>Route attribute access on a class to __getattr__.</p>790<p>This is a descriptor, used to define attributes that act differently when791accessed through an instance and through a class. Instance access remains792normal, but access to an attribute through a class will be routed to the793class’s __getattr__ method; this is done by raising AttributeError.</p>794<p>This allows one to have properties active on an instance, and have virtual795attributes on the class with the same name (see <a class="reference internal" href="enum.html#enum.Enum" title="enum.Enum"><code class="xref py py-class docutils literal notranslate"><span class="pre">enum.Enum</span></code></a> for an example).</p>796<div class="versionadded">797<p><span class="versionmodified added">Added in version 3.4.</span></p>798</div>799</dd></dl>800 801</section>802<section id="coroutine-utility-functions">803<h2>Coroutine Utility Functions<a class="headerlink" href="#coroutine-utility-functions" title="Link to this heading">¶</a></h2>804<dl class="py function">805<dt class="sig sig-object py" id="types.coroutine">806<span class="sig-prename descclassname"><span class="pre">types.</span></span><span class="sig-name descname"><span class="pre">coroutine</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">gen_func</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#types.coroutine" title="Link to this definition">¶</a></dt>807<dd><p>This function transforms a <a class="reference internal" href="../glossary.html#term-generator"><span class="xref std std-term">generator</span></a> function into a808<a class="reference internal" href="../glossary.html#term-coroutine-function"><span class="xref std std-term">coroutine function</span></a> which returns a generator-based coroutine.809The generator-based coroutine is still a <a class="reference internal" href="../glossary.html#term-generator-iterator"><span class="xref std std-term">generator iterator</span></a>,810but is also considered to be a <a class="reference internal" href="../glossary.html#term-coroutine"><span class="xref std std-term">coroutine</span></a> object and is811<a class="reference internal" href="../glossary.html#term-awaitable"><span class="xref std std-term">awaitable</span></a>. However, it may not necessarily implement812the <a class="reference internal" href="../reference/datamodel.html#object.__await__" title="object.__await__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__await__()</span></code></a> method.</p>813<p>If <em>gen_func</em> is a generator function, it will be modified in-place.</p>814<p>If <em>gen_func</em> is not a generator function, it will be wrapped. If it815returns an instance of <a class="reference internal" href="collections.abc.html#collections.abc.Generator" title="collections.abc.Generator"><code class="xref py py-class docutils literal notranslate"><span class="pre">collections.abc.Generator</span></code></a>, the instance816will be wrapped in an <em>awaitable</em> proxy object. All other types817of objects will be returned as is.</p>818<div class="versionadded">819<p><span class="versionmodified added">Added in version 3.5.</span></p>820</div>821</dd></dl>822 823</section>824</section>825 826 827 <div class="clearer"></div>828 </div>829 </div>830 </div>831 <div class="sphinxsidebar" role="navigation" aria-label="Main">832 <div class="sphinxsidebarwrapper">833 <div>834 <h3><a href="../contents.html">Table of Contents</a></h3>835 <ul>836<li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">types</span></code> — Dynamic type creation and names for built-in types</a><ul>837<li><a class="reference internal" href="#dynamic-type-creation">Dynamic Type Creation</a></li>838<li><a class="reference internal" href="#standard-interpreter-types">Standard Interpreter Types</a></li>839<li><a class="reference internal" href="#additional-utility-classes-and-functions">Additional Utility Classes and Functions</a></li>840<li><a class="reference internal" href="#coroutine-utility-functions">Coroutine Utility Functions</a></li>841</ul>842</li>843</ul>844 845 </div>846 <div>847 <h4>Previous topic</h4>848 <p class="topless"><a href="weakref.html"849 title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">weakref</span></code> — Weak references</a></p>850 </div>851 <div>852 <h4>Next topic</h4>853 <p class="topless"><a href="copy.html"854 title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">copy</span></code> — Shallow and deep copy operations</a></p>855 </div>856 <script>857 document.addEventListener('DOMContentLoaded', () => {858 const title = document.querySelector('meta[property="og:title"]').content;859 const elements = document.querySelectorAll('.improvepage');860 const pageurl = window.location.href.split('?')[0];861 elements.forEach(element => {862 const url = new URL(element.href.split('?')[0].replace("-nojs", ""));863 url.searchParams.set('pagetitle', title);864 url.searchParams.set('pageurl', pageurl);865 url.searchParams.set('pagesource', "library/types.rst");866 element.href = url.toString();867 });868 });869 </script>870 <div role="note" aria-label="source link">871 <h3>This page</h3>872 <ul class="this-page-menu">873 <li><a href="../bugs.html">Report a bug</a></li>874 <li><a class="improvepage" href="../improve-page-nojs.html">Improve this page</a></li>875 <li>876 <a href="https://github.com/python/cpython/blob/main/Doc/library/types.rst?plain=1"877 rel="nofollow">Show source878 </a>879 </li>880 881 </ul>882 </div>883 </div>884<div id="sidebarbutton" title="Collapse sidebar">885<span>«</span>886</div>887 888 </div>889 <div class="clearer"></div>890 </div> 891 <div class="related" role="navigation" aria-label="Related">892 <h3>Navigation</h3>893 <ul>894 <li class="right" style="margin-right: 10px">895 <a href="../genindex.html" title="General Index"896 >index</a></li>897 <li class="right" >898 <a href="../py-modindex.html" title="Python Module Index"899 >modules</a> |</li>900 <li class="right" >901 <a href="copy.html" title="copy — Shallow and deep copy operations"902 >next</a> |</li>903 <li class="right" >904 <a href="weakref.html" title="weakref — Weak references"905 >previous</a> |</li>906 907 <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"></li>908 <li><a href="https://www.python.org/">Python</a> »</li>909 <li class="switchers">910 <div class="language_switcher_placeholder"></div>911 <div class="version_switcher_placeholder"></div>912 </li>913 <li>914 915 </li>916 <li id="cpython-language-and-version">917 <a href="../index.html">3.15.0a6 Documentation</a> »918 </li>919 920 <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>921 <li class="nav-item nav-item-2"><a href="datatypes.html" >Data Types</a> »</li>922 <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">types</span></code> — Dynamic type creation and names for built-in types</a></li>923 <li class="right">924 925 926 <div class="inline-search" role="search">927 <form class="inline-search" action="../search.html" method="get">928 <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box">929 <input type="submit" value="Go">930 </form>931 </div>932 |933 </li>934 <li class="right">935<label class="theme-selector-label">936 Theme937 <select class="theme-selector" oninput="activateTheme(this.value)">938 <option value="auto" selected>Auto</option>939 <option value="light">Light</option>940 <option value="dark">Dark</option>941 </select>942</label> |</li>943 944 </ul>945 </div> 946 <div class="footer">947 © <a href="../copyright.html">Copyright</a> 2001 Python Software Foundation.948 <br>949 This page is licensed under the Python Software Foundation License Version 2.950 <br>951 Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.952 <br>953 954 See <a href="/license.html">History and License</a> for more information.<br>955 956 957 <br>958 959 The Python Software Foundation is a non-profit corporation.960<a href="https://www.python.org/psf/donations/">Please donate.</a>961<br>962 <br>963 Last updated on Mar 10, 2026 (08:58 UTC).964 965 <a href="/bugs.html">Found a bug</a>?966 967 <br>968 969 Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 8.2.3.970 </div>971 972 </body>973</html>