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

1232 line
110KB

  1. <!DOCTYPE html>
  2. <html lang="en" data-content_root="../">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
  6. <meta property="og:title" content="Common Object Structures" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/c-api/structures.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="There are a large number of structures which are used in the definition of object types for Python. This section describes these structures and how they are used. Base object types and macros: All ..." />
  11. <meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
  12. <meta property="og:image:alt" content="Python documentation" />
  13. <meta name="description" content="There are a large number of structures which are used in the definition of object types for Python. This section describes these structures and how they are used. Base object types and macros: All ..." />
  14. <meta property="og:image:width" content="200" />
  15. <meta property="og:image:height" content="200" />
  16. <meta name="theme-color" content="#3776ab" />
  17. <title>Common Object Structures &#8212; Python 3.12.3 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=80d5e7a1" />
  19. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=bb723527" />
  20. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=b20cc3f5" />
  21. <script src="../_static/documentation_options.js?v=2c828074"></script>
  22. <script src="../_static/doctools.js?v=888ff710"></script>
  23. <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
  24. <script src="../_static/sidebar.js"></script>
  25. <link rel="search" type="application/opensearchdescription+xml"
  26. title="Search within Python 3.12.3 documentation"
  27. href="../_static/opensearch.xml"/>
  28. <link rel="author" title="About these documents" href="../about.html" />
  29. <link rel="index" title="Index" href="../genindex.html" />
  30. <link rel="search" title="Search" href="../search.html" />
  31. <link rel="copyright" title="Copyright" href="../copyright.html" />
  32. <link rel="next" title="Type Objects" href="typeobj.html" />
  33. <link rel="prev" title="Allocating Objects on the Heap" href="allocation.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/c-api/structures.html" />
  35. <style>
  36. @media only screen {
  37. table.full-width-table {
  38. width: 100%;
  39. }
  40. }
  41. </style>
  42. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  43. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  44. <script type="text/javascript" src="../_static/copybutton.js"></script>
  45. <script type="text/javascript" src="../_static/menu.js"></script>
  46. <script type="text/javascript" src="../_static/search-focus.js"></script>
  47. <script type="text/javascript" src="../_static/themetoggle.js"></script>
  48. </head>
  49. <body>
  50. <div class="mobile-nav">
  51. <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
  52. aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
  53. <nav class="nav-content" role="navigation">
  54. <label for="menuToggler" class="toggler__label">
  55. <span></span>
  56. </label>
  57. <span class="nav-items-wrapper">
  58. <a href="https://www.python.org/" class="nav-logo">
  59. <img src="../_static/py.svg" alt="Python logo"/>
  60. </a>
  61. <span class="version_switcher_placeholder"></span>
  62. <form role="search" class="search" action="../search.html" method="get">
  63. <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
  64. <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>
  65. </svg>
  66. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  67. <input type="submit" value="Go"/>
  68. </form>
  69. </span>
  70. </nav>
  71. <div class="menu-wrapper">
  72. <nav class="menu" role="navigation" aria-label="main navigation">
  73. <div class="language_switcher_placeholder"></div>
  74. <label class="theme-selector-label">
  75. Theme
  76. <select class="theme-selector" oninput="activateTheme(this.value)">
  77. <option value="auto" selected>Auto</option>
  78. <option value="light">Light</option>
  79. <option value="dark">Dark</option>
  80. </select>
  81. </label>
  82. <div>
  83. <h3><a href="../contents.html">Table of Contents</a></h3>
  84. <ul>
  85. <li><a class="reference internal" href="#">Common Object Structures</a><ul>
  86. <li><a class="reference internal" href="#base-object-types-and-macros">Base object types and macros</a></li>
  87. <li><a class="reference internal" href="#implementing-functions-and-methods">Implementing functions and methods</a></li>
  88. <li><a class="reference internal" href="#accessing-attributes-of-extension-types">Accessing attributes of extension types</a><ul>
  89. <li><a class="reference internal" href="#member-flags">Member flags</a></li>
  90. <li><a class="reference internal" href="#member-types">Member types</a></li>
  91. <li><a class="reference internal" href="#defining-getters-and-setters">Defining Getters and Setters</a></li>
  92. </ul>
  93. </li>
  94. </ul>
  95. </li>
  96. </ul>
  97. </div>
  98. <div>
  99. <h4>Previous topic</h4>
  100. <p class="topless"><a href="allocation.html"
  101. title="previous chapter">Allocating Objects on the Heap</a></p>
  102. </div>
  103. <div>
  104. <h4>Next topic</h4>
  105. <p class="topless"><a href="typeobj.html"
  106. title="next chapter">Type Objects</a></p>
  107. </div>
  108. <div role="note" aria-label="source link">
  109. <h3>This Page</h3>
  110. <ul class="this-page-menu">
  111. <li><a href="../bugs.html">Report a Bug</a></li>
  112. <li>
  113. <a href="https://github.com/python/cpython/blob/main/Doc/c-api/structures.rst"
  114. rel="nofollow">Show Source
  115. </a>
  116. </li>
  117. </ul>
  118. </div>
  119. </nav>
  120. </div>
  121. </div>
  122. <div class="related" role="navigation" aria-label="related navigation">
  123. <h3>Navigation</h3>
  124. <ul>
  125. <li class="right" style="margin-right: 10px">
  126. <a href="../genindex.html" title="General Index"
  127. accesskey="I">index</a></li>
  128. <li class="right" >
  129. <a href="../py-modindex.html" title="Python Module Index"
  130. >modules</a> |</li>
  131. <li class="right" >
  132. <a href="typeobj.html" title="Type Objects"
  133. accesskey="N">next</a> |</li>
  134. <li class="right" >
  135. <a href="allocation.html" title="Allocating Objects on the Heap"
  136. accesskey="P">previous</a> |</li>
  137. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  138. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  139. <li class="switchers">
  140. <div class="language_switcher_placeholder"></div>
  141. <div class="version_switcher_placeholder"></div>
  142. </li>
  143. <li>
  144. </li>
  145. <li id="cpython-language-and-version">
  146. <a href="../index.html">3.12.3 Documentation</a> &#187;
  147. </li>
  148. <li class="nav-item nav-item-1"><a href="index.html" >Python/C API Reference Manual</a> &#187;</li>
  149. <li class="nav-item nav-item-2"><a href="objimpl.html" accesskey="U">Object Implementation Support</a> &#187;</li>
  150. <li class="nav-item nav-item-this"><a href="">Common Object Structures</a></li>
  151. <li class="right">
  152. <div class="inline-search" role="search">
  153. <form class="inline-search" action="../search.html" method="get">
  154. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  155. <input type="submit" value="Go" />
  156. </form>
  157. </div>
  158. |
  159. </li>
  160. <li class="right">
  161. <label class="theme-selector-label">
  162. Theme
  163. <select class="theme-selector" oninput="activateTheme(this.value)">
  164. <option value="auto" selected>Auto</option>
  165. <option value="light">Light</option>
  166. <option value="dark">Dark</option>
  167. </select>
  168. </label> |</li>
  169. </ul>
  170. </div>
  171. <div class="document">
  172. <div class="documentwrapper">
  173. <div class="bodywrapper">
  174. <div class="body" role="main">
  175. <section id="common-object-structures">
  176. <span id="common-structs"></span><h1>Common Object Structures<a class="headerlink" href="#common-object-structures" title="Link to this heading">¶</a></h1>
  177. <p>There are a large number of structures which are used in the definition of
  178. object types for Python. This section describes these structures and how they
  179. are used.</p>
  180. <section id="base-object-types-and-macros">
  181. <h2>Base object types and macros<a class="headerlink" href="#base-object-types-and-macros" title="Link to this heading">¶</a></h2>
  182. <p>All Python objects ultimately share a small number of fields at the beginning
  183. of the object’s representation in memory. These are represented by the
  184. <a class="reference internal" href="#c.PyObject" title="PyObject"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyObject</span></code></a> and <a class="reference internal" href="#c.PyVarObject" title="PyVarObject"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyVarObject</span></code></a> types, which are defined, in turn,
  185. by the expansions of some macros also used, whether directly or indirectly, in
  186. the definition of all other Python objects. Additional macros can be found
  187. under <a class="reference internal" href="refcounting.html#countingrefs"><span class="std std-ref">reference counting</span></a>.</p>
  188. <dl class="c type">
  189. <dt class="sig sig-object c" id="c.PyObject">
  190. <span class="k"><span class="pre">type</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyObject</span></span></span><a class="headerlink" href="#c.PyObject" title="Link to this definition">¶</a><br /></dt>
  191. <dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Limited API</span></a>. (Only some members are part of the stable ABI.)</em><p>All object types are extensions of this type. This is a type which
  192. contains the information Python needs to treat a pointer to an object as an
  193. object. In a normal “release” build, it contains only the object’s
  194. reference count and a pointer to the corresponding type object.
  195. Nothing is actually declared to be a <a class="reference internal" href="#c.PyObject" title="PyObject"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyObject</span></code></a>, but every pointer
  196. to a Python object can be cast to a <span class="c-expr sig sig-inline c"><a class="reference internal" href="#c.PyObject" title="PyObject"><span class="n">PyObject</span></a><span class="p">*</span></span>. Access to the
  197. members must be done by using the macros <a class="reference internal" href="refcounting.html#c.Py_REFCNT" title="Py_REFCNT"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_REFCNT</span></code></a> and
  198. <a class="reference internal" href="#c.Py_TYPE" title="Py_TYPE"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_TYPE</span></code></a>.</p>
  199. </dd></dl>
  200. <dl class="c type">
  201. <dt class="sig sig-object c" id="c.PyVarObject">
  202. <span class="k"><span class="pre">type</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyVarObject</span></span></span><a class="headerlink" href="#c.PyVarObject" title="Link to this definition">¶</a><br /></dt>
  203. <dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Limited API</span></a>. (Only some members are part of the stable ABI.)</em><p>This is an extension of <a class="reference internal" href="#c.PyObject" title="PyObject"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyObject</span></code></a> that adds the <a class="reference internal" href="typeobj.html#c.PyVarObject.ob_size" title="PyVarObject.ob_size"><code class="xref c c-member docutils literal notranslate"><span class="pre">ob_size</span></code></a>
  204. field. This is only used for objects that have some notion of <em>length</em>.
  205. This type does not often appear in the Python/C API.
  206. Access to the members must be done by using the macros
  207. <a class="reference internal" href="refcounting.html#c.Py_REFCNT" title="Py_REFCNT"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_REFCNT</span></code></a>, <a class="reference internal" href="#c.Py_TYPE" title="Py_TYPE"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_TYPE</span></code></a>, and <a class="reference internal" href="#c.Py_SIZE" title="Py_SIZE"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_SIZE</span></code></a>.</p>
  208. </dd></dl>
  209. <dl class="c macro">
  210. <dt class="sig sig-object c" id="c.PyObject_HEAD">
  211. <span class="sig-name descname"><span class="n"><span class="pre">PyObject_HEAD</span></span></span><a class="headerlink" href="#c.PyObject_HEAD" title="Link to this definition">¶</a><br /></dt>
  212. <dd><p>This is a macro used when declaring new types which represent objects
  213. without a varying length. The PyObject_HEAD macro expands to:</p>
  214. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">PyObject</span><span class="w"> </span><span class="n">ob_base</span><span class="p">;</span>
  215. </pre></div>
  216. </div>
  217. <p>See documentation of <a class="reference internal" href="#c.PyObject" title="PyObject"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyObject</span></code></a> above.</p>
  218. </dd></dl>
  219. <dl class="c macro">
  220. <dt class="sig sig-object c" id="c.PyObject_VAR_HEAD">
  221. <span class="sig-name descname"><span class="n"><span class="pre">PyObject_VAR_HEAD</span></span></span><a class="headerlink" href="#c.PyObject_VAR_HEAD" title="Link to this definition">¶</a><br /></dt>
  222. <dd><p>This is a macro used when declaring new types which represent objects
  223. with a length that varies from instance to instance.
  224. The PyObject_VAR_HEAD macro expands to:</p>
  225. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">PyVarObject</span><span class="w"> </span><span class="n">ob_base</span><span class="p">;</span>
  226. </pre></div>
  227. </div>
  228. <p>See documentation of <a class="reference internal" href="#c.PyVarObject" title="PyVarObject"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyVarObject</span></code></a> above.</p>
  229. </dd></dl>
  230. <dl class="c function">
  231. <dt class="sig sig-object c" id="c.Py_Is">
  232. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">Py_Is</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">x</span></span>, <a class="reference internal" href="#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">y</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_Is" title="Link to this definition">¶</a><br /></dt>
  233. <dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a> since version 3.10.</em><p>Test if the <em>x</em> object is the <em>y</em> object, the same as <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">is</span> <span class="pre">y</span></code> in Python.</p>
  234. <div class="versionadded">
  235. <p><span class="versionmodified added">New in version 3.10.</span></p>
  236. </div>
  237. </dd></dl>
  238. <dl class="c function">
  239. <dt class="sig sig-object c" id="c.Py_IsNone">
  240. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">Py_IsNone</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">x</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_IsNone" title="Link to this definition">¶</a><br /></dt>
  241. <dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a> since version 3.10.</em><p>Test if an object is the <code class="docutils literal notranslate"><span class="pre">None</span></code> singleton,
  242. the same as <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">is</span> <span class="pre">None</span></code> in Python.</p>
  243. <div class="versionadded">
  244. <p><span class="versionmodified added">New in version 3.10.</span></p>
  245. </div>
  246. </dd></dl>
  247. <dl class="c function">
  248. <dt class="sig sig-object c" id="c.Py_IsTrue">
  249. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">Py_IsTrue</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">x</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_IsTrue" title="Link to this definition">¶</a><br /></dt>
  250. <dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a> since version 3.10.</em><p>Test if an object is the <code class="docutils literal notranslate"><span class="pre">True</span></code> singleton,
  251. the same as <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">is</span> <span class="pre">True</span></code> in Python.</p>
  252. <div class="versionadded">
  253. <p><span class="versionmodified added">New in version 3.10.</span></p>
  254. </div>
  255. </dd></dl>
  256. <dl class="c function">
  257. <dt class="sig sig-object c" id="c.Py_IsFalse">
  258. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">Py_IsFalse</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">x</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_IsFalse" title="Link to this definition">¶</a><br /></dt>
  259. <dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a> since version 3.10.</em><p>Test if an object is the <code class="docutils literal notranslate"><span class="pre">False</span></code> singleton,
  260. the same as <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">is</span> <span class="pre">False</span></code> in Python.</p>
  261. <div class="versionadded">
  262. <p><span class="versionmodified added">New in version 3.10.</span></p>
  263. </div>
  264. </dd></dl>
  265. <dl class="c function">
  266. <dt class="sig sig-object c" id="c.Py_TYPE">
  267. <a class="reference internal" href="type.html#c.PyTypeObject" title="PyTypeObject"><span class="n"><span class="pre">PyTypeObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">Py_TYPE</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">o</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_TYPE" title="Link to this definition">¶</a><br /></dt>
  268. <dd><p>Get the type of the Python object <em>o</em>.</p>
  269. <p>Return a <a class="reference internal" href="../glossary.html#term-borrowed-reference"><span class="xref std std-term">borrowed reference</span></a>.</p>
  270. <p>Use the <a class="reference internal" href="#c.Py_SET_TYPE" title="Py_SET_TYPE"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_SET_TYPE()</span></code></a> function to set an object type.</p>
  271. <div class="versionchanged">
  272. <p><span class="versionmodified changed">Changed in version 3.11: </span><a class="reference internal" href="#c.Py_TYPE" title="Py_TYPE"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_TYPE()</span></code></a> is changed to an inline static function.
  273. The parameter type is no longer <span class="c-expr sig sig-inline c"><span class="k">const</span><span class="w"> </span><a class="reference internal" href="#c.PyObject" title="PyObject"><span class="n">PyObject</span></a><span class="p">*</span></span>.</p>
  274. </div>
  275. </dd></dl>
  276. <dl class="c function">
  277. <dt class="sig sig-object c" id="c.Py_IS_TYPE">
  278. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">Py_IS_TYPE</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">o</span></span>, <a class="reference internal" href="type.html#c.PyTypeObject" title="PyTypeObject"><span class="n"><span class="pre">PyTypeObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">type</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_IS_TYPE" title="Link to this definition">¶</a><br /></dt>
  279. <dd><p>Return non-zero if the object <em>o</em> type is <em>type</em>. Return zero otherwise.
  280. Equivalent to: <code class="docutils literal notranslate"><span class="pre">Py_TYPE(o)</span> <span class="pre">==</span> <span class="pre">type</span></code>.</p>
  281. <div class="versionadded">
  282. <p><span class="versionmodified added">New in version 3.9.</span></p>
  283. </div>
  284. </dd></dl>
  285. <dl class="c function">
  286. <dt class="sig sig-object c" id="c.Py_SET_TYPE">
  287. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">Py_SET_TYPE</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">o</span></span>, <a class="reference internal" href="type.html#c.PyTypeObject" title="PyTypeObject"><span class="n"><span class="pre">PyTypeObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">type</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_SET_TYPE" title="Link to this definition">¶</a><br /></dt>
  288. <dd><p>Set the object <em>o</em> type to <em>type</em>.</p>
  289. <div class="versionadded">
  290. <p><span class="versionmodified added">New in version 3.9.</span></p>
  291. </div>
  292. </dd></dl>
  293. <dl class="c function">
  294. <dt class="sig sig-object c" id="c.Py_SIZE">
  295. <a class="reference internal" href="intro.html#c.Py_ssize_t" title="Py_ssize_t"><span class="n"><span class="pre">Py_ssize_t</span></span></a><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">Py_SIZE</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="#c.PyVarObject" title="PyVarObject"><span class="n"><span class="pre">PyVarObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">o</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_SIZE" title="Link to this definition">¶</a><br /></dt>
  296. <dd><p>Get the size of the Python object <em>o</em>.</p>
  297. <p>Use the <a class="reference internal" href="#c.Py_SET_SIZE" title="Py_SET_SIZE"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_SET_SIZE()</span></code></a> function to set an object size.</p>
  298. <div class="versionchanged">
  299. <p><span class="versionmodified changed">Changed in version 3.11: </span><a class="reference internal" href="#c.Py_SIZE" title="Py_SIZE"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_SIZE()</span></code></a> is changed to an inline static function.
  300. The parameter type is no longer <span class="c-expr sig sig-inline c"><span class="k">const</span><span class="w"> </span><a class="reference internal" href="#c.PyVarObject" title="PyVarObject"><span class="n">PyVarObject</span></a><span class="p">*</span></span>.</p>
  301. </div>
  302. </dd></dl>
  303. <dl class="c function">
  304. <dt class="sig sig-object c" id="c.Py_SET_SIZE">
  305. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">Py_SET_SIZE</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="#c.PyVarObject" title="PyVarObject"><span class="n"><span class="pre">PyVarObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">o</span></span>, <a class="reference internal" href="intro.html#c.Py_ssize_t" title="Py_ssize_t"><span class="n"><span class="pre">Py_ssize_t</span></span></a><span class="w"> </span><span class="n"><span class="pre">size</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_SET_SIZE" title="Link to this definition">¶</a><br /></dt>
  306. <dd><p>Set the object <em>o</em> size to <em>size</em>.</p>
  307. <div class="versionadded">
  308. <p><span class="versionmodified added">New in version 3.9.</span></p>
  309. </div>
  310. </dd></dl>
  311. <dl class="c macro">
  312. <dt class="sig sig-object c" id="c.PyObject_HEAD_INIT">
  313. <span class="sig-name descname"><span class="n"><span class="pre">PyObject_HEAD_INIT</span></span></span><span class="sig-paren">(</span><span class="n"><span class="pre">type</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyObject_HEAD_INIT" title="Link to this definition">¶</a><br /></dt>
  314. <dd><p>This is a macro which expands to initialization values for a new
  315. <a class="reference internal" href="#c.PyObject" title="PyObject"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyObject</span></code></a> type. This macro expands to:</p>
  316. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">_PyObject_EXTRA_INIT</span>
  317. <span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="n">type</span><span class="p">,</span>
  318. </pre></div>
  319. </div>
  320. </dd></dl>
  321. <dl class="c macro">
  322. <dt class="sig sig-object c" id="c.PyVarObject_HEAD_INIT">
  323. <span class="sig-name descname"><span class="n"><span class="pre">PyVarObject_HEAD_INIT</span></span></span><span class="sig-paren">(</span><span class="n"><span class="pre">type</span></span>, <span class="n"><span class="pre">size</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyVarObject_HEAD_INIT" title="Link to this definition">¶</a><br /></dt>
  324. <dd><p>This is a macro which expands to initialization values for a new
  325. <a class="reference internal" href="#c.PyVarObject" title="PyVarObject"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyVarObject</span></code></a> type, including the <a class="reference internal" href="typeobj.html#c.PyVarObject.ob_size" title="PyVarObject.ob_size"><code class="xref c c-member docutils literal notranslate"><span class="pre">ob_size</span></code></a> field.
  326. This macro expands to:</p>
  327. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">_PyObject_EXTRA_INIT</span>
  328. <span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="n">type</span><span class="p">,</span><span class="w"> </span><span class="n">size</span><span class="p">,</span>
  329. </pre></div>
  330. </div>
  331. </dd></dl>
  332. </section>
  333. <section id="implementing-functions-and-methods">
  334. <h2>Implementing functions and methods<a class="headerlink" href="#implementing-functions-and-methods" title="Link to this heading">¶</a></h2>
  335. <dl class="c type">
  336. <dt class="sig sig-object c" id="c.PyCFunction">
  337. <span class="k"><span class="pre">type</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyCFunction</span></span></span><a class="headerlink" href="#c.PyCFunction" title="Link to this definition">¶</a><br /></dt>
  338. <dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a>.</em><p>Type of the functions used to implement most Python callables in C.
  339. Functions of this type take two <span class="c-expr sig sig-inline c"><a class="reference internal" href="#c.PyObject" title="PyObject"><span class="n">PyObject</span></a><span class="p">*</span></span> parameters and return
  340. one such value. If the return value is <code class="docutils literal notranslate"><span class="pre">NULL</span></code>, an exception shall have
  341. been set. If not <code class="docutils literal notranslate"><span class="pre">NULL</span></code>, the return value is interpreted as the return
  342. value of the function as exposed in Python. The function must return a new
  343. reference.</p>
  344. <p>The function signature is:</p>
  345. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">PyObject</span><span class="w"> </span><span class="o">*</span><span class="nf">PyCFunction</span><span class="p">(</span><span class="n">PyObject</span><span class="w"> </span><span class="o">*</span><span class="n">self</span><span class="p">,</span>
  346. <span class="w"> </span><span class="n">PyObject</span><span class="w"> </span><span class="o">*</span><span class="n">args</span><span class="p">);</span>
  347. </pre></div>
  348. </div>
  349. </dd></dl>
  350. <dl class="c type">
  351. <dt class="sig sig-object c" id="c.PyCFunctionWithKeywords">
  352. <span class="k"><span class="pre">type</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyCFunctionWithKeywords</span></span></span><a class="headerlink" href="#c.PyCFunctionWithKeywords" title="Link to this definition">¶</a><br /></dt>
  353. <dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a>.</em><p>Type of the functions used to implement Python callables in C
  354. with signature <a class="reference internal" href="#meth-varargs-meth-keywords"><span class="std std-ref">METH_VARARGS | METH_KEYWORDS</span></a>.
  355. The function signature is:</p>
  356. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">PyObject</span><span class="w"> </span><span class="o">*</span><span class="nf">PyCFunctionWithKeywords</span><span class="p">(</span><span class="n">PyObject</span><span class="w"> </span><span class="o">*</span><span class="n">self</span><span class="p">,</span>
  357. <span class="w"> </span><span class="n">PyObject</span><span class="w"> </span><span class="o">*</span><span class="n">args</span><span class="p">,</span>
  358. <span class="w"> </span><span class="n">PyObject</span><span class="w"> </span><span class="o">*</span><span class="n">kwargs</span><span class="p">);</span>
  359. </pre></div>
  360. </div>
  361. </dd></dl>
  362. <dl class="c type">
  363. <dt class="sig sig-object c" id="c._PyCFunctionFast">
  364. <span class="k"><span class="pre">type</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">_PyCFunctionFast</span></span></span><a class="headerlink" href="#c._PyCFunctionFast" title="Link to this definition">¶</a><br /></dt>
  365. <dd><p>Type of the functions used to implement Python callables in C
  366. with signature <a class="reference internal" href="#c.METH_FASTCALL" title="METH_FASTCALL"><code class="xref c c-macro docutils literal notranslate"><span class="pre">METH_FASTCALL</span></code></a>.
  367. The function signature is:</p>
  368. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">PyObject</span><span class="w"> </span><span class="o">*</span><span class="nf">_PyCFunctionFast</span><span class="p">(</span><span class="n">PyObject</span><span class="w"> </span><span class="o">*</span><span class="n">self</span><span class="p">,</span>
  369. <span class="w"> </span><span class="n">PyObject</span><span class="w"> </span><span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="o">*</span><span class="n">args</span><span class="p">,</span>
  370. <span class="w"> </span><span class="n">Py_ssize_t</span><span class="w"> </span><span class="n">nargs</span><span class="p">);</span>
  371. </pre></div>
  372. </div>
  373. </dd></dl>
  374. <dl class="c type">
  375. <dt class="sig sig-object c" id="c._PyCFunctionFastWithKeywords">
  376. <span class="k"><span class="pre">type</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">_PyCFunctionFastWithKeywords</span></span></span><a class="headerlink" href="#c._PyCFunctionFastWithKeywords" title="Link to this definition">¶</a><br /></dt>
  377. <dd><p>Type of the functions used to implement Python callables in C
  378. with signature <a class="reference internal" href="#meth-fastcall-meth-keywords"><span class="std std-ref">METH_FASTCALL | METH_KEYWORDS</span></a>.
  379. The function signature is:</p>
  380. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">PyObject</span><span class="w"> </span><span class="o">*</span><span class="nf">_PyCFunctionFastWithKeywords</span><span class="p">(</span><span class="n">PyObject</span><span class="w"> </span><span class="o">*</span><span class="n">self</span><span class="p">,</span>
  381. <span class="w"> </span><span class="n">PyObject</span><span class="w"> </span><span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="o">*</span><span class="n">args</span><span class="p">,</span>
  382. <span class="w"> </span><span class="n">Py_ssize_t</span><span class="w"> </span><span class="n">nargs</span><span class="p">,</span>
  383. <span class="w"> </span><span class="n">PyObject</span><span class="w"> </span><span class="o">*</span><span class="n">kwnames</span><span class="p">);</span>
  384. </pre></div>
  385. </div>
  386. </dd></dl>
  387. <dl class="c type">
  388. <dt class="sig sig-object c" id="c.PyCMethod">
  389. <span class="k"><span class="pre">type</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyCMethod</span></span></span><a class="headerlink" href="#c.PyCMethod" title="Link to this definition">¶</a><br /></dt>
  390. <dd><p>Type of the functions used to implement Python callables in C
  391. with signature <a class="reference internal" href="#meth-method-meth-fastcall-meth-keywords"><span class="std std-ref">METH_METHOD | METH_FASTCALL | METH_KEYWORDS</span></a>.
  392. The function signature is:</p>
  393. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">PyObject</span><span class="w"> </span><span class="o">*</span><span class="n">PyCMethod</span><span class="p">(</span><span class="n">PyObject</span><span class="w"> </span><span class="o">*</span><span class="n">self</span><span class="p">,</span>
  394. <span class="w"> </span><span class="n">PyTypeObject</span><span class="w"> </span><span class="o">*</span><span class="n">defining_class</span><span class="p">,</span>
  395. <span class="w"> </span><span class="n">PyObject</span><span class="w"> </span><span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="o">*</span><span class="n">args</span><span class="p">,</span>
  396. <span class="w"> </span><span class="n">Py_ssize_t</span><span class="w"> </span><span class="n">nargs</span><span class="p">,</span>
  397. <span class="w"> </span><span class="n">PyObject</span><span class="w"> </span><span class="o">*</span><span class="n">kwnames</span><span class="p">)</span>
  398. </pre></div>
  399. </div>
  400. <div class="versionadded">
  401. <p><span class="versionmodified added">New in version 3.9.</span></p>
  402. </div>
  403. </dd></dl>
  404. <dl class="c type">
  405. <dt class="sig sig-object c" id="c.PyMethodDef">
  406. <span class="k"><span class="pre">type</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyMethodDef</span></span></span><a class="headerlink" href="#c.PyMethodDef" title="Link to this definition">¶</a><br /></dt>
  407. <dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a> (including all members).</em><p>Structure used to describe a method of an extension type. This structure has
  408. four fields:</p>
  409. <dl class="c member">
  410. <dt class="sig sig-object c" id="c.PyMethodDef.ml_name">
  411. <span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">ml_name</span></span></span><a class="headerlink" href="#c.PyMethodDef.ml_name" title="Link to this definition">¶</a><br /></dt>
  412. <dd><p>Name of the method.</p>
  413. </dd></dl>
  414. <dl class="c member">
  415. <dt class="sig sig-object c" id="c.PyMethodDef.ml_meth">
  416. <a class="reference internal" href="#c.PyCFunction" title="PyCFunction"><span class="n"><span class="pre">PyCFunction</span></span></a><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">ml_meth</span></span></span><a class="headerlink" href="#c.PyMethodDef.ml_meth" title="Link to this definition">¶</a><br /></dt>
  417. <dd><p>Pointer to the C implementation.</p>
  418. </dd></dl>
  419. <dl class="c member">
  420. <dt class="sig sig-object c" id="c.PyMethodDef.ml_flags">
  421. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">ml_flags</span></span></span><a class="headerlink" href="#c.PyMethodDef.ml_flags" title="Link to this definition">¶</a><br /></dt>
  422. <dd><p>Flags bits indicating how the call should be constructed.</p>
  423. </dd></dl>
  424. <dl class="c member">
  425. <dt class="sig sig-object c" id="c.PyMethodDef.ml_doc">
  426. <span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">ml_doc</span></span></span><a class="headerlink" href="#c.PyMethodDef.ml_doc" title="Link to this definition">¶</a><br /></dt>
  427. <dd><p>Points to the contents of the docstring.</p>
  428. </dd></dl>
  429. </dd></dl>
  430. <p>The <a class="reference internal" href="#c.PyMethodDef.ml_meth" title="PyMethodDef.ml_meth"><code class="xref c c-member docutils literal notranslate"><span class="pre">ml_meth</span></code></a> is a C function pointer.
  431. The functions may be of different
  432. types, but they always return <span class="c-expr sig sig-inline c"><a class="reference internal" href="#c.PyObject" title="PyObject"><span class="n">PyObject</span></a><span class="p">*</span></span>. If the function is not of
  433. the <a class="reference internal" href="#c.PyCFunction" title="PyCFunction"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyCFunction</span></code></a>, the compiler will require a cast in the method table.
  434. Even though <a class="reference internal" href="#c.PyCFunction" title="PyCFunction"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyCFunction</span></code></a> defines the first parameter as
  435. <span class="c-expr sig sig-inline c"><a class="reference internal" href="#c.PyObject" title="PyObject"><span class="n">PyObject</span></a><span class="p">*</span></span>, it is common that the method implementation uses the
  436. specific C type of the <em>self</em> object.</p>
  437. <p>The <a class="reference internal" href="#c.PyMethodDef.ml_flags" title="PyMethodDef.ml_flags"><code class="xref c c-member docutils literal notranslate"><span class="pre">ml_flags</span></code></a> field is a bitfield which can include
  438. the following flags.
  439. The individual flags indicate either a calling convention or a binding
  440. convention.</p>
  441. <p>There are these calling conventions:</p>
  442. <dl class="c macro">
  443. <dt class="sig sig-object c" id="c.METH_VARARGS">
  444. <span class="sig-name descname"><span class="n"><span class="pre">METH_VARARGS</span></span></span><a class="headerlink" href="#c.METH_VARARGS" title="Link to this definition">¶</a><br /></dt>
  445. <dd><p>This is the typical calling convention, where the methods have the type
  446. <a class="reference internal" href="#c.PyCFunction" title="PyCFunction"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyCFunction</span></code></a>. The function expects two <span class="c-expr sig sig-inline c"><a class="reference internal" href="#c.PyObject" title="PyObject"><span class="n">PyObject</span></a><span class="p">*</span></span> values.
  447. The first one is the <em>self</em> object for methods; for module functions, it is
  448. the module object. The second parameter (often called <em>args</em>) is a tuple
  449. object representing all arguments. This parameter is typically processed
  450. using <a class="reference internal" href="arg.html#c.PyArg_ParseTuple" title="PyArg_ParseTuple"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyArg_ParseTuple()</span></code></a> or <a class="reference internal" href="arg.html#c.PyArg_UnpackTuple" title="PyArg_UnpackTuple"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyArg_UnpackTuple()</span></code></a>.</p>
  451. </dd></dl>
  452. <dl class="c macro">
  453. <dt class="sig sig-object c" id="c.METH_KEYWORDS">
  454. <span class="sig-name descname"><span class="n"><span class="pre">METH_KEYWORDS</span></span></span><a class="headerlink" href="#c.METH_KEYWORDS" title="Link to this definition">¶</a><br /></dt>
  455. <dd><p>Can only be used in certain combinations with other flags:
  456. <a class="reference internal" href="#meth-varargs-meth-keywords"><span class="std std-ref">METH_VARARGS | METH_KEYWORDS</span></a>,
  457. <a class="reference internal" href="#meth-fastcall-meth-keywords"><span class="std std-ref">METH_FASTCALL | METH_KEYWORDS</span></a> and
  458. <a class="reference internal" href="#meth-method-meth-fastcall-meth-keywords"><span class="std std-ref">METH_METHOD | METH_FASTCALL | METH_KEYWORDS</span></a>.</p>
  459. </dd></dl>
  460. <dl class="simple" id="meth-varargs-meth-keywords">
  461. <dt><span class="c-expr sig sig-inline c"><a class="reference internal" href="#c.METH_VARARGS" title="METH_VARARGS"><span class="n">METH_VARARGS</span></a><span class="w"> </span><span class="o">|</span><span class="w"> </span><a class="reference internal" href="#c.METH_KEYWORDS" title="METH_KEYWORDS"><span class="n">METH_KEYWORDS</span></a></span></dt><dd><p>Methods with these flags must be of type <a class="reference internal" href="#c.PyCFunctionWithKeywords" title="PyCFunctionWithKeywords"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyCFunctionWithKeywords</span></code></a>.
  462. The function expects three parameters: <em>self</em>, <em>args</em>, <em>kwargs</em> where
  463. <em>kwargs</em> is a dictionary of all the keyword arguments or possibly <code class="docutils literal notranslate"><span class="pre">NULL</span></code>
  464. if there are no keyword arguments. The parameters are typically processed
  465. using <a class="reference internal" href="arg.html#c.PyArg_ParseTupleAndKeywords" title="PyArg_ParseTupleAndKeywords"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyArg_ParseTupleAndKeywords()</span></code></a>.</p>
  466. </dd>
  467. </dl>
  468. <dl class="c macro">
  469. <dt class="sig sig-object c" id="c.METH_FASTCALL">
  470. <span class="sig-name descname"><span class="n"><span class="pre">METH_FASTCALL</span></span></span><a class="headerlink" href="#c.METH_FASTCALL" title="Link to this definition">¶</a><br /></dt>
  471. <dd><p>Fast calling convention supporting only positional arguments.
  472. The methods have the type <a class="reference internal" href="#c._PyCFunctionFast" title="_PyCFunctionFast"><code class="xref c c-type docutils literal notranslate"><span class="pre">_PyCFunctionFast</span></code></a>.
  473. The first parameter is <em>self</em>, the second parameter is a C array
  474. of <span class="c-expr sig sig-inline c"><a class="reference internal" href="#c.PyObject" title="PyObject"><span class="n">PyObject</span></a><span class="p">*</span></span> values indicating the arguments and the third
  475. parameter is the number of arguments (the length of the array).</p>
  476. <div class="versionadded">
  477. <p><span class="versionmodified added">New in version 3.7.</span></p>
  478. </div>
  479. <div class="versionchanged">
  480. <p><span class="versionmodified changed">Changed in version 3.10: </span><code class="docutils literal notranslate"><span class="pre">METH_FASTCALL</span></code> is now part of the <a class="reference internal" href="stable.html#stable-abi"><span class="std std-ref">stable ABI</span></a>.</p>
  481. </div>
  482. </dd></dl>
  483. <dl id="meth-fastcall-meth-keywords">
  484. <dt><span class="c-expr sig sig-inline c"><a class="reference internal" href="#c.METH_FASTCALL" title="METH_FASTCALL"><span class="n">METH_FASTCALL</span></a><span class="w"> </span><span class="o">|</span><span class="w"> </span><a class="reference internal" href="#c.METH_KEYWORDS" title="METH_KEYWORDS"><span class="n">METH_KEYWORDS</span></a></span></dt><dd><p>Extension of <a class="reference internal" href="#c.METH_FASTCALL" title="METH_FASTCALL"><code class="xref c c-macro docutils literal notranslate"><span class="pre">METH_FASTCALL</span></code></a> supporting also keyword arguments,
  485. with methods of type <a class="reference internal" href="#c._PyCFunctionFastWithKeywords" title="_PyCFunctionFastWithKeywords"><code class="xref c c-type docutils literal notranslate"><span class="pre">_PyCFunctionFastWithKeywords</span></code></a>.
  486. Keyword arguments are passed the same way as in the
  487. <a class="reference internal" href="call.html#vectorcall"><span class="std std-ref">vectorcall protocol</span></a>:
  488. there is an additional fourth <span class="c-expr sig sig-inline c"><a class="reference internal" href="#c.PyObject" title="PyObject"><span class="n">PyObject</span></a><span class="p">*</span></span> parameter
  489. which is a tuple representing the names of the keyword arguments
  490. (which are guaranteed to be strings)
  491. or possibly <code class="docutils literal notranslate"><span class="pre">NULL</span></code> if there are no keywords. The values of the keyword
  492. arguments are stored in the <em>args</em> array, after the positional arguments.</p>
  493. <div class="versionadded">
  494. <p><span class="versionmodified added">New in version 3.7.</span></p>
  495. </div>
  496. </dd>
  497. </dl>
  498. <dl class="c macro">
  499. <dt class="sig sig-object c" id="c.METH_METHOD">
  500. <span class="sig-name descname"><span class="n"><span class="pre">METH_METHOD</span></span></span><a class="headerlink" href="#c.METH_METHOD" title="Link to this definition">¶</a><br /></dt>
  501. <dd><p>Can only be used in the combination with other flags:
  502. <a class="reference internal" href="#meth-method-meth-fastcall-meth-keywords"><span class="std std-ref">METH_METHOD | METH_FASTCALL | METH_KEYWORDS</span></a>.</p>
  503. </dd></dl>
  504. <dl id="meth-method-meth-fastcall-meth-keywords">
  505. <dt><span class="c-expr sig sig-inline c"><a class="reference internal" href="#c.METH_METHOD" title="METH_METHOD"><span class="n">METH_METHOD</span></a><span class="w"> </span><span class="o">|</span><span class="w"> </span><a class="reference internal" href="#c.METH_FASTCALL" title="METH_FASTCALL"><span class="n">METH_FASTCALL</span></a><span class="w"> </span><span class="o">|</span><span class="w"> </span><a class="reference internal" href="#c.METH_KEYWORDS" title="METH_KEYWORDS"><span class="n">METH_KEYWORDS</span></a></span></dt><dd><p>Extension of <a class="reference internal" href="#meth-fastcall-meth-keywords"><span class="std std-ref">METH_FASTCALL | METH_KEYWORDS</span></a>
  506. supporting the <em>defining class</em>, that is,
  507. the class that contains the method in question.
  508. The defining class might be a superclass of <code class="docutils literal notranslate"><span class="pre">Py_TYPE(self)</span></code>.</p>
  509. <p>The method needs to be of type <a class="reference internal" href="#c.PyCMethod" title="PyCMethod"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyCMethod</span></code></a>, the same as for
  510. <code class="docutils literal notranslate"><span class="pre">METH_FASTCALL</span> <span class="pre">|</span> <span class="pre">METH_KEYWORDS</span></code> with <code class="docutils literal notranslate"><span class="pre">defining_class</span></code> argument added after
  511. <code class="docutils literal notranslate"><span class="pre">self</span></code>.</p>
  512. <div class="versionadded">
  513. <p><span class="versionmodified added">New in version 3.9.</span></p>
  514. </div>
  515. </dd>
  516. </dl>
  517. <dl class="c macro">
  518. <dt class="sig sig-object c" id="c.METH_NOARGS">
  519. <span class="sig-name descname"><span class="n"><span class="pre">METH_NOARGS</span></span></span><a class="headerlink" href="#c.METH_NOARGS" title="Link to this definition">¶</a><br /></dt>
  520. <dd><p>Methods without parameters don’t need to check whether arguments are given if
  521. they are listed with the <a class="reference internal" href="#c.METH_NOARGS" title="METH_NOARGS"><code class="xref c c-macro docutils literal notranslate"><span class="pre">METH_NOARGS</span></code></a> flag. They need to be of type
  522. <a class="reference internal" href="#c.PyCFunction" title="PyCFunction"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyCFunction</span></code></a>. The first parameter is typically named <em>self</em> and will
  523. hold a reference to the module or object instance. In all cases the second
  524. parameter will be <code class="docutils literal notranslate"><span class="pre">NULL</span></code>.</p>
  525. <p>The function must have 2 parameters. Since the second parameter is unused,
  526. <a class="reference internal" href="intro.html#c.Py_UNUSED" title="Py_UNUSED"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_UNUSED</span></code></a> can be used to prevent a compiler warning.</p>
  527. </dd></dl>
  528. <dl class="c macro">
  529. <dt class="sig sig-object c" id="c.METH_O">
  530. <span class="sig-name descname"><span class="n"><span class="pre">METH_O</span></span></span><a class="headerlink" href="#c.METH_O" title="Link to this definition">¶</a><br /></dt>
  531. <dd><p>Methods with a single object argument can be listed with the <a class="reference internal" href="#c.METH_O" title="METH_O"><code class="xref c c-macro docutils literal notranslate"><span class="pre">METH_O</span></code></a>
  532. flag, instead of invoking <a class="reference internal" href="arg.html#c.PyArg_ParseTuple" title="PyArg_ParseTuple"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyArg_ParseTuple()</span></code></a> with a <code class="docutils literal notranslate"><span class="pre">&quot;O&quot;</span></code> argument.
  533. They have the type <a class="reference internal" href="#c.PyCFunction" title="PyCFunction"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyCFunction</span></code></a>, with the <em>self</em> parameter, and a
  534. <span class="c-expr sig sig-inline c"><a class="reference internal" href="#c.PyObject" title="PyObject"><span class="n">PyObject</span></a><span class="p">*</span></span> parameter representing the single argument.</p>
  535. </dd></dl>
  536. <p>These two constants are not used to indicate the calling convention but the
  537. binding when use with methods of classes. These may not be used for functions
  538. defined for modules. At most one of these flags may be set for any given
  539. method.</p>
  540. <dl class="c macro">
  541. <dt class="sig sig-object c" id="c.METH_CLASS">
  542. <span class="sig-name descname"><span class="n"><span class="pre">METH_CLASS</span></span></span><a class="headerlink" href="#c.METH_CLASS" title="Link to this definition">¶</a><br /></dt>
  543. <dd><p id="index-0">The method will be passed the type object as the first parameter rather
  544. than an instance of the type. This is used to create <em>class methods</em>,
  545. similar to what is created when using the <a class="reference internal" href="../library/functions.html#classmethod" title="classmethod"><code class="xref py py-func docutils literal notranslate"><span class="pre">classmethod()</span></code></a> built-in
  546. function.</p>
  547. </dd></dl>
  548. <dl class="c macro">
  549. <dt class="sig sig-object c" id="c.METH_STATIC">
  550. <span class="sig-name descname"><span class="n"><span class="pre">METH_STATIC</span></span></span><a class="headerlink" href="#c.METH_STATIC" title="Link to this definition">¶</a><br /></dt>
  551. <dd><p id="index-1">The method will be passed <code class="docutils literal notranslate"><span class="pre">NULL</span></code> as the first parameter rather than an
  552. instance of the type. This is used to create <em>static methods</em>, similar to
  553. what is created when using the <a class="reference internal" href="../library/functions.html#staticmethod" title="staticmethod"><code class="xref py py-func docutils literal notranslate"><span class="pre">staticmethod()</span></code></a> built-in function.</p>
  554. </dd></dl>
  555. <p>One other constant controls whether a method is loaded in place of another
  556. definition with the same method name.</p>
  557. <dl class="c macro">
  558. <dt class="sig sig-object c" id="c.METH_COEXIST">
  559. <span class="sig-name descname"><span class="n"><span class="pre">METH_COEXIST</span></span></span><a class="headerlink" href="#c.METH_COEXIST" title="Link to this definition">¶</a><br /></dt>
  560. <dd><p>The method will be loaded in place of existing definitions. Without
  561. <em>METH_COEXIST</em>, the default is to skip repeated definitions. Since slot
  562. wrappers are loaded before the method table, the existence of a
  563. <em>sq_contains</em> slot, for example, would generate a wrapped method named
  564. <a class="reference internal" href="../reference/datamodel.html#object.__contains__" title="object.__contains__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__contains__()</span></code></a> and preclude the loading of a corresponding
  565. PyCFunction with the same name. With the flag defined, the PyCFunction
  566. will be loaded in place of the wrapper object and will co-exist with the
  567. slot. This is helpful because calls to PyCFunctions are optimized more
  568. than wrapper object calls.</p>
  569. </dd></dl>
  570. <dl class="c function">
  571. <dt class="sig sig-object c" id="c.PyCMethod_New">
  572. <a class="reference internal" href="#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyCMethod_New</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="#c.PyMethodDef" title="PyMethodDef"><span class="n"><span class="pre">PyMethodDef</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">ml</span></span>, <a class="reference internal" href="#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">self</span></span>, <a class="reference internal" href="#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">module</span></span>, <a class="reference internal" href="type.html#c.PyTypeObject" title="PyTypeObject"><span class="n"><span class="pre">PyTypeObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">cls</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyCMethod_New" title="Link to this definition">¶</a><br /></dt>
  573. <dd><em class="refcount">Return value: New reference.</em><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a> since version 3.9.</em><p>Turn <em>ml</em> into a Python <a class="reference internal" href="../glossary.html#term-callable"><span class="xref std std-term">callable</span></a> object.
  574. The caller must ensure that <em>ml</em> outlives the <a class="reference internal" href="../glossary.html#term-callable"><span class="xref std std-term">callable</span></a>.
  575. Typically, <em>ml</em> is defined as a static variable.</p>
  576. <p>The <em>self</em> parameter will be passed as the <em>self</em> argument
  577. to the C function in <code class="docutils literal notranslate"><span class="pre">ml-&gt;ml_meth</span></code> when invoked.
  578. <em>self</em> can be <code class="docutils literal notranslate"><span class="pre">NULL</span></code>.</p>
  579. <p>The <a class="reference internal" href="../glossary.html#term-callable"><span class="xref std std-term">callable</span></a> object’s <code class="docutils literal notranslate"><span class="pre">__module__</span></code> attribute
  580. can be set from the given <em>module</em> argument.
  581. <em>module</em> should be a Python string,
  582. which will be used as name of the module the function is defined in.
  583. If unavailable, it can be set to <a class="reference internal" href="../library/constants.html#None" title="None"><code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code></a> or <code class="docutils literal notranslate"><span class="pre">NULL</span></code>.</p>
  584. <div class="admonition seealso">
  585. <p class="admonition-title">See also</p>
  586. <p><a class="reference internal" href="../reference/datamodel.html#function.__module__" title="function.__module__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">function.__module__</span></code></a></p>
  587. </div>
  588. <p>The <em>cls</em> parameter will be passed as the <em>defining_class</em>
  589. argument to the C function.
  590. Must be set if <a class="reference internal" href="#c.METH_METHOD" title="METH_METHOD"><code class="xref c c-macro docutils literal notranslate"><span class="pre">METH_METHOD</span></code></a> is set on <code class="docutils literal notranslate"><span class="pre">ml-&gt;ml_flags</span></code>.</p>
  591. <div class="versionadded">
  592. <p><span class="versionmodified added">New in version 3.9.</span></p>
  593. </div>
  594. </dd></dl>
  595. <dl class="c function">
  596. <dt class="sig sig-object c" id="c.PyCFunction_NewEx">
  597. <a class="reference internal" href="#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyCFunction_NewEx</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="#c.PyMethodDef" title="PyMethodDef"><span class="n"><span class="pre">PyMethodDef</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">ml</span></span>, <a class="reference internal" href="#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">self</span></span>, <a class="reference internal" href="#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">module</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyCFunction_NewEx" title="Link to this definition">¶</a><br /></dt>
  598. <dd><em class="refcount">Return value: New reference.</em><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a>.</em><p>Equivalent to <code class="docutils literal notranslate"><span class="pre">PyCMethod_New(ml,</span> <span class="pre">self,</span> <span class="pre">module,</span> <span class="pre">NULL)</span></code>.</p>
  599. </dd></dl>
  600. <dl class="c function">
  601. <dt class="sig sig-object c" id="c.PyCFunction_New">
  602. <a class="reference internal" href="#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyCFunction_New</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="#c.PyMethodDef" title="PyMethodDef"><span class="n"><span class="pre">PyMethodDef</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">ml</span></span>, <a class="reference internal" href="#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">self</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyCFunction_New" title="Link to this definition">¶</a><br /></dt>
  603. <dd><em class="refcount">Return value: New reference.</em><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a> since version 3.4.</em><p>Equivalent to <code class="docutils literal notranslate"><span class="pre">PyCMethod_New(ml,</span> <span class="pre">self,</span> <span class="pre">NULL,</span> <span class="pre">NULL)</span></code>.</p>
  604. </dd></dl>
  605. </section>
  606. <section id="accessing-attributes-of-extension-types">
  607. <h2>Accessing attributes of extension types<a class="headerlink" href="#accessing-attributes-of-extension-types" title="Link to this heading">¶</a></h2>
  608. <dl class="c type">
  609. <dt class="sig sig-object c" id="c.PyMemberDef">
  610. <span class="k"><span class="pre">type</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyMemberDef</span></span></span><a class="headerlink" href="#c.PyMemberDef" title="Link to this definition">¶</a><br /></dt>
  611. <dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a> (including all members).</em><p>Structure which describes an attribute of a type which corresponds to a C
  612. struct member.
  613. When defining a class, put a NULL-terminated array of these
  614. structures in the <a class="reference internal" href="typeobj.html#c.PyTypeObject.tp_members" title="PyTypeObject.tp_members"><code class="xref c c-member docutils literal notranslate"><span class="pre">tp_members</span></code></a> slot.</p>
  615. <p>Its fields are, in order:</p>
  616. <dl class="c member">
  617. <dt class="sig sig-object c" id="c.PyMemberDef.name">
  618. <span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">name</span></span></span><a class="headerlink" href="#c.PyMemberDef.name" title="Link to this definition">¶</a><br /></dt>
  619. <dd><p>Name of the member.
  620. A NULL value marks the end of a <code class="docutils literal notranslate"><span class="pre">PyMemberDef[]</span></code> array.</p>
  621. <p>The string should be static, no copy is made of it.</p>
  622. </dd></dl>
  623. <dl class="c member">
  624. <dt class="sig sig-object c" id="c.PyMemberDef.type">
  625. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">type</span></span></span><a class="headerlink" href="#c.PyMemberDef.type" title="Link to this definition">¶</a><br /></dt>
  626. <dd><p>The type of the member in the C struct.
  627. See <a class="reference internal" href="#pymemberdef-types"><span class="std std-ref">Member types</span></a> for the possible values.</p>
  628. </dd></dl>
  629. <dl class="c member">
  630. <dt class="sig sig-object c" id="c.PyMemberDef.offset">
  631. <a class="reference internal" href="intro.html#c.Py_ssize_t" title="Py_ssize_t"><span class="n"><span class="pre">Py_ssize_t</span></span></a><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">offset</span></span></span><a class="headerlink" href="#c.PyMemberDef.offset" title="Link to this definition">¶</a><br /></dt>
  632. <dd><p>The offset in bytes that the member is located on the type’s object struct.</p>
  633. </dd></dl>
  634. <dl class="c member">
  635. <dt class="sig sig-object c" id="c.PyMemberDef.flags">
  636. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">flags</span></span></span><a class="headerlink" href="#c.PyMemberDef.flags" title="Link to this definition">¶</a><br /></dt>
  637. <dd><p>Zero or more of the <a class="reference internal" href="#pymemberdef-flags"><span class="std std-ref">Member flags</span></a>, combined using bitwise OR.</p>
  638. </dd></dl>
  639. <dl class="c member">
  640. <dt class="sig sig-object c" id="c.PyMemberDef.doc">
  641. <span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">doc</span></span></span><a class="headerlink" href="#c.PyMemberDef.doc" title="Link to this definition">¶</a><br /></dt>
  642. <dd><p>The docstring, or NULL.
  643. The string should be static, no copy is made of it.
  644. Typically, it is defined using <a class="reference internal" href="intro.html#c.PyDoc_STR" title="PyDoc_STR"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PyDoc_STR</span></code></a>.</p>
  645. </dd></dl>
  646. <p>By default (when <a class="reference internal" href="#c.PyMemberDef.flags" title="PyMemberDef.flags"><code class="xref c c-member docutils literal notranslate"><span class="pre">flags</span></code></a> is <code class="docutils literal notranslate"><span class="pre">0</span></code>), members allow
  647. both read and write access.
  648. Use the <a class="reference internal" href="#c.Py_READONLY" title="Py_READONLY"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_READONLY</span></code></a> flag for read-only access.
  649. Certain types, like <a class="reference internal" href="#c.Py_T_STRING" title="Py_T_STRING"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_T_STRING</span></code></a>, imply <a class="reference internal" href="#c.Py_READONLY" title="Py_READONLY"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_READONLY</span></code></a>.
  650. Only <a class="reference internal" href="#c.Py_T_OBJECT_EX" title="Py_T_OBJECT_EX"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_T_OBJECT_EX</span></code></a> (and legacy <a class="reference internal" href="#c.T_OBJECT" title="T_OBJECT"><code class="xref c c-macro docutils literal notranslate"><span class="pre">T_OBJECT</span></code></a>) members can
  651. be deleted.</p>
  652. <p id="pymemberdef-offsets">For heap-allocated types (created using <a class="reference internal" href="type.html#c.PyType_FromSpec" title="PyType_FromSpec"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyType_FromSpec()</span></code></a> or similar),
  653. <code class="docutils literal notranslate"><span class="pre">PyMemberDef</span></code> may contain a definition for the special member
  654. <code class="docutils literal notranslate"><span class="pre">&quot;__vectorcalloffset__&quot;</span></code>, corresponding to
  655. <a class="reference internal" href="typeobj.html#c.PyTypeObject.tp_vectorcall_offset" title="PyTypeObject.tp_vectorcall_offset"><code class="xref c c-member docutils literal notranslate"><span class="pre">tp_vectorcall_offset</span></code></a> in type objects.
  656. These must be defined with <code class="docutils literal notranslate"><span class="pre">Py_T_PYSSIZET</span></code> and <code class="docutils literal notranslate"><span class="pre">Py_READONLY</span></code>, for example:</p>
  657. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">static</span><span class="w"> </span><span class="n">PyMemberDef</span><span class="w"> </span><span class="n">spam_type_members</span><span class="p">[]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span>
  658. <span class="w"> </span><span class="p">{</span><span class="s">&quot;__vectorcalloffset__&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">Py_T_PYSSIZET</span><span class="p">,</span>
  659. <span class="w"> </span><span class="n">offsetof</span><span class="p">(</span><span class="n">Spam_object</span><span class="p">,</span><span class="w"> </span><span class="n">vectorcall</span><span class="p">),</span><span class="w"> </span><span class="n">Py_READONLY</span><span class="p">},</span>
  660. <span class="w"> </span><span class="p">{</span><span class="nb">NULL</span><span class="p">}</span><span class="w"> </span><span class="cm">/* Sentinel */</span>
  661. <span class="p">};</span>
  662. </pre></div>
  663. </div>
  664. <p>(You may need to <code class="docutils literal notranslate"><span class="pre">#include</span> <span class="pre">&lt;stddef.h&gt;</span></code> for <code class="xref c c-func docutils literal notranslate"><span class="pre">offsetof()</span></code>.)</p>
  665. <p>The legacy offsets <a class="reference internal" href="typeobj.html#c.PyTypeObject.tp_dictoffset" title="PyTypeObject.tp_dictoffset"><code class="xref c c-member docutils literal notranslate"><span class="pre">tp_dictoffset</span></code></a> and
  666. <a class="reference internal" href="typeobj.html#c.PyTypeObject.tp_weaklistoffset" title="PyTypeObject.tp_weaklistoffset"><code class="xref c c-member docutils literal notranslate"><span class="pre">tp_weaklistoffset</span></code></a> can be defined similarly using
  667. <code class="docutils literal notranslate"><span class="pre">&quot;__dictoffset__&quot;</span></code> and <code class="docutils literal notranslate"><span class="pre">&quot;__weaklistoffset__&quot;</span></code> members, but extensions
  668. are strongly encouraged to use <a class="reference internal" href="typeobj.html#c.Py_TPFLAGS_MANAGED_DICT" title="Py_TPFLAGS_MANAGED_DICT"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_TPFLAGS_MANAGED_DICT</span></code></a> and
  669. <a class="reference internal" href="typeobj.html#c.Py_TPFLAGS_MANAGED_WEAKREF" title="Py_TPFLAGS_MANAGED_WEAKREF"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_TPFLAGS_MANAGED_WEAKREF</span></code></a> instead.</p>
  670. <div class="versionchanged">
  671. <p><span class="versionmodified changed">Changed in version 3.12: </span><code class="docutils literal notranslate"><span class="pre">PyMemberDef</span></code> is always available.
  672. Previously, it required including <code class="docutils literal notranslate"><span class="pre">&quot;structmember.h&quot;</span></code>.</p>
  673. </div>
  674. </dd></dl>
  675. <dl class="c function">
  676. <dt class="sig sig-object c" id="c.PyMember_GetOne">
  677. <a class="reference internal" href="#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyMember_GetOne</span></span></span><span class="sig-paren">(</span><span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">obj_addr</span></span>, <span class="k"><span class="pre">struct</span></span><span class="w"> </span><a class="reference internal" href="#c.PyMemberDef" title="PyMemberDef"><span class="n"><span class="pre">PyMemberDef</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">m</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyMember_GetOne" title="Link to this definition">¶</a><br /></dt>
  678. <dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a>.</em><p>Get an attribute belonging to the object at address <em>obj_addr</em>. The
  679. attribute is described by <code class="docutils literal notranslate"><span class="pre">PyMemberDef</span></code> <em>m</em>. Returns <code class="docutils literal notranslate"><span class="pre">NULL</span></code>
  680. on error.</p>
  681. <div class="versionchanged">
  682. <p><span class="versionmodified changed">Changed in version 3.12: </span><code class="docutils literal notranslate"><span class="pre">PyMember_GetOne</span></code> is always available.
  683. Previously, it required including <code class="docutils literal notranslate"><span class="pre">&quot;structmember.h&quot;</span></code>.</p>
  684. </div>
  685. </dd></dl>
  686. <dl class="c function">
  687. <dt class="sig sig-object c" id="c.PyMember_SetOne">
  688. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyMember_SetOne</span></span></span><span class="sig-paren">(</span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">obj_addr</span></span>, <span class="k"><span class="pre">struct</span></span><span class="w"> </span><a class="reference internal" href="#c.PyMemberDef" title="PyMemberDef"><span class="n"><span class="pre">PyMemberDef</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">m</span></span>, <a class="reference internal" href="#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">o</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyMember_SetOne" title="Link to this definition">¶</a><br /></dt>
  689. <dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a>.</em><p>Set an attribute belonging to the object at address <em>obj_addr</em> to object <em>o</em>.
  690. The attribute to set is described by <code class="docutils literal notranslate"><span class="pre">PyMemberDef</span></code> <em>m</em>. Returns <code class="docutils literal notranslate"><span class="pre">0</span></code>
  691. if successful and a negative value on failure.</p>
  692. <div class="versionchanged">
  693. <p><span class="versionmodified changed">Changed in version 3.12: </span><code class="docutils literal notranslate"><span class="pre">PyMember_SetOne</span></code> is always available.
  694. Previously, it required including <code class="docutils literal notranslate"><span class="pre">&quot;structmember.h&quot;</span></code>.</p>
  695. </div>
  696. </dd></dl>
  697. <section id="member-flags">
  698. <span id="pymemberdef-flags"></span><h3>Member flags<a class="headerlink" href="#member-flags" title="Link to this heading">¶</a></h3>
  699. <p>The following flags can be used with <a class="reference internal" href="#c.PyMemberDef.flags" title="PyMemberDef.flags"><code class="xref c c-member docutils literal notranslate"><span class="pre">PyMemberDef.flags</span></code></a>:</p>
  700. <dl class="c macro">
  701. <dt class="sig sig-object c" id="c.Py_READONLY">
  702. <span class="sig-name descname"><span class="n"><span class="pre">Py_READONLY</span></span></span><a class="headerlink" href="#c.Py_READONLY" title="Link to this definition">¶</a><br /></dt>
  703. <dd><p>Not writable.</p>
  704. </dd></dl>
  705. <dl class="c macro">
  706. <dt class="sig sig-object c" id="c.Py_AUDIT_READ">
  707. <span class="sig-name descname"><span class="n"><span class="pre">Py_AUDIT_READ</span></span></span><a class="headerlink" href="#c.Py_AUDIT_READ" title="Link to this definition">¶</a><br /></dt>
  708. <dd><p>Emit an <code class="docutils literal notranslate"><span class="pre">object.__getattr__</span></code> <a class="reference internal" href="../library/audit_events.html#audit-events"><span class="std std-ref">audit event</span></a>
  709. before reading.</p>
  710. </dd></dl>
  711. <dl class="c macro">
  712. <dt class="sig sig-object c" id="c.Py_RELATIVE_OFFSET">
  713. <span class="sig-name descname"><span class="n"><span class="pre">Py_RELATIVE_OFFSET</span></span></span><a class="headerlink" href="#c.Py_RELATIVE_OFFSET" title="Link to this definition">¶</a><br /></dt>
  714. <dd><p>Indicates that the <a class="reference internal" href="#c.PyMemberDef.offset" title="PyMemberDef.offset"><code class="xref c c-member docutils literal notranslate"><span class="pre">offset</span></code></a> of this <code class="docutils literal notranslate"><span class="pre">PyMemberDef</span></code>
  715. entry indicates an offset from the subclass-specific data, rather than
  716. from <code class="docutils literal notranslate"><span class="pre">PyObject</span></code>.</p>
  717. <p>Can only be used as part of <a class="reference internal" href="typeobj.html#c.PyTypeObject.tp_members" title="PyTypeObject.tp_members"><code class="xref c c-member docutils literal notranslate"><span class="pre">Py_tp_members</span></code></a>
  718. <a class="reference internal" href="type.html#c.PyType_Slot" title="PyType_Slot"><code class="xref c c-type docutils literal notranslate"><span class="pre">slot</span></code></a> when creating a class using negative
  719. <a class="reference internal" href="type.html#c.PyType_Spec.basicsize" title="PyType_Spec.basicsize"><code class="xref c c-member docutils literal notranslate"><span class="pre">basicsize</span></code></a>.
  720. It is mandatory in that case.</p>
  721. <p>This flag is only used in <a class="reference internal" href="type.html#c.PyType_Slot" title="PyType_Slot"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyType_Slot</span></code></a>.
  722. When setting <a class="reference internal" href="typeobj.html#c.PyTypeObject.tp_members" title="PyTypeObject.tp_members"><code class="xref c c-member docutils literal notranslate"><span class="pre">tp_members</span></code></a> during
  723. class creation, Python clears it and sets
  724. <a class="reference internal" href="#c.PyMemberDef.offset" title="PyMemberDef.offset"><code class="xref c c-member docutils literal notranslate"><span class="pre">PyMemberDef.offset</span></code></a> to the offset from the <code class="docutils literal notranslate"><span class="pre">PyObject</span></code> struct.</p>
  725. </dd></dl>
  726. <div class="versionchanged" id="index-2">
  727. <p><span class="versionmodified changed">Changed in version 3.10: </span>The <code class="xref c c-macro docutils literal notranslate"><span class="pre">RESTRICTED</span></code>, <code class="xref c c-macro docutils literal notranslate"><span class="pre">READ_RESTRICTED</span></code> and
  728. <code class="xref c c-macro docutils literal notranslate"><span class="pre">WRITE_RESTRICTED</span></code> macros available with
  729. <code class="docutils literal notranslate"><span class="pre">#include</span> <span class="pre">&quot;structmember.h&quot;</span></code> are deprecated.
  730. <code class="xref c c-macro docutils literal notranslate"><span class="pre">READ_RESTRICTED</span></code> and <code class="xref c c-macro docutils literal notranslate"><span class="pre">RESTRICTED</span></code> are equivalent to
  731. <a class="reference internal" href="#c.Py_AUDIT_READ" title="Py_AUDIT_READ"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_AUDIT_READ</span></code></a>; <code class="xref c c-macro docutils literal notranslate"><span class="pre">WRITE_RESTRICTED</span></code> does nothing.</p>
  732. </div>
  733. <div class="versionchanged" id="index-3">
  734. <p><span class="versionmodified changed">Changed in version 3.12: </span>The <code class="xref c c-macro docutils literal notranslate"><span class="pre">READONLY</span></code> macro was renamed to <a class="reference internal" href="#c.Py_READONLY" title="Py_READONLY"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_READONLY</span></code></a>.
  735. The <code class="xref c c-macro docutils literal notranslate"><span class="pre">PY_AUDIT_READ</span></code> macro was renamed with the <code class="docutils literal notranslate"><span class="pre">Py_</span></code> prefix.
  736. The new names are now always available.
  737. Previously, these required <code class="docutils literal notranslate"><span class="pre">#include</span> <span class="pre">&quot;structmember.h&quot;</span></code>.
  738. The header is still available and it provides the old names.</p>
  739. </div>
  740. </section>
  741. <section id="member-types">
  742. <span id="pymemberdef-types"></span><h3>Member types<a class="headerlink" href="#member-types" title="Link to this heading">¶</a></h3>
  743. <p><a class="reference internal" href="#c.PyMemberDef.type" title="PyMemberDef.type"><code class="xref c c-member docutils literal notranslate"><span class="pre">PyMemberDef.type</span></code></a> can be one of the following macros corresponding
  744. to various C types.
  745. When the member is accessed in Python, it will be converted to the
  746. equivalent Python type.
  747. When it is set from Python, it will be converted back to the C type.
  748. If that is not possible, an exception such as <a class="reference internal" href="../library/exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> or
  749. <a class="reference internal" href="../library/exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> is raised.</p>
  750. <p>Unless marked (D), attributes defined this way cannot be deleted
  751. using e.g. <a class="reference internal" href="../reference/simple_stmts.html#del"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">del</span></code></a> or <a class="reference internal" href="../library/functions.html#delattr" title="delattr"><code class="xref py py-func docutils literal notranslate"><span class="pre">delattr()</span></code></a>.</p>
  752. <table class="docutils align-default">
  753. <thead>
  754. <tr class="row-odd"><th class="head"><p>Macro name</p></th>
  755. <th class="head"><p>C type</p></th>
  756. <th class="head"><p>Python type</p></th>
  757. </tr>
  758. </thead>
  759. <tbody>
  760. <tr class="row-even"><td><dl class="c macro">
  761. <dt class="sig sig-object c" id="c.Py_T_BYTE">
  762. <span class="sig-name descname"><span class="n"><span class="pre">Py_T_BYTE</span></span></span><a class="headerlink" href="#c.Py_T_BYTE" title="Link to this definition">¶</a><br /></dt>
  763. <dd></dd></dl>
  764. </td>
  765. <td><p><span class="c-expr sig sig-inline c"><span class="kt">char</span></span></p></td>
  766. <td><p><a class="reference internal" href="../library/functions.html#int" title="int"><code class="xref py py-class docutils literal notranslate"><span class="pre">int</span></code></a></p></td>
  767. </tr>
  768. <tr class="row-odd"><td><dl class="c macro">
  769. <dt class="sig sig-object c" id="c.Py_T_SHORT">
  770. <span class="sig-name descname"><span class="n"><span class="pre">Py_T_SHORT</span></span></span><a class="headerlink" href="#c.Py_T_SHORT" title="Link to this definition">¶</a><br /></dt>
  771. <dd></dd></dl>
  772. </td>
  773. <td><p><span class="c-expr sig sig-inline c"><span class="kt">short</span></span></p></td>
  774. <td><p><a class="reference internal" href="../library/functions.html#int" title="int"><code class="xref py py-class docutils literal notranslate"><span class="pre">int</span></code></a></p></td>
  775. </tr>
  776. <tr class="row-even"><td><dl class="c macro">
  777. <dt class="sig sig-object c" id="c.Py_T_INT">
  778. <span class="sig-name descname"><span class="n"><span class="pre">Py_T_INT</span></span></span><a class="headerlink" href="#c.Py_T_INT" title="Link to this definition">¶</a><br /></dt>
  779. <dd></dd></dl>
  780. </td>
  781. <td><p><span class="c-expr sig sig-inline c"><span class="kt">int</span></span></p></td>
  782. <td><p><a class="reference internal" href="../library/functions.html#int" title="int"><code class="xref py py-class docutils literal notranslate"><span class="pre">int</span></code></a></p></td>
  783. </tr>
  784. <tr class="row-odd"><td><dl class="c macro">
  785. <dt class="sig sig-object c" id="c.Py_T_LONG">
  786. <span class="sig-name descname"><span class="n"><span class="pre">Py_T_LONG</span></span></span><a class="headerlink" href="#c.Py_T_LONG" title="Link to this definition">¶</a><br /></dt>
  787. <dd></dd></dl>
  788. </td>
  789. <td><p><span class="c-expr sig sig-inline c"><span class="kt">long</span></span></p></td>
  790. <td><p><a class="reference internal" href="../library/functions.html#int" title="int"><code class="xref py py-class docutils literal notranslate"><span class="pre">int</span></code></a></p></td>
  791. </tr>
  792. <tr class="row-even"><td><dl class="c macro">
  793. <dt class="sig sig-object c" id="c.Py_T_LONGLONG">
  794. <span class="sig-name descname"><span class="n"><span class="pre">Py_T_LONGLONG</span></span></span><a class="headerlink" href="#c.Py_T_LONGLONG" title="Link to this definition">¶</a><br /></dt>
  795. <dd></dd></dl>
  796. </td>
  797. <td><p><span class="c-expr sig sig-inline c"><span class="kt">long</span><span class="w"> </span><span class="kt">long</span></span></p></td>
  798. <td><p><a class="reference internal" href="../library/functions.html#int" title="int"><code class="xref py py-class docutils literal notranslate"><span class="pre">int</span></code></a></p></td>
  799. </tr>
  800. <tr class="row-odd"><td><dl class="c macro">
  801. <dt class="sig sig-object c" id="c.Py_T_UBYTE">
  802. <span class="sig-name descname"><span class="n"><span class="pre">Py_T_UBYTE</span></span></span><a class="headerlink" href="#c.Py_T_UBYTE" title="Link to this definition">¶</a><br /></dt>
  803. <dd></dd></dl>
  804. </td>
  805. <td><p><span class="c-expr sig sig-inline c"><span class="kt">unsigned</span><span class="w"> </span><span class="kt">char</span></span></p></td>
  806. <td><p><a class="reference internal" href="../library/functions.html#int" title="int"><code class="xref py py-class docutils literal notranslate"><span class="pre">int</span></code></a></p></td>
  807. </tr>
  808. <tr class="row-even"><td><dl class="c macro">
  809. <dt class="sig sig-object c" id="c.Py_T_UINT">
  810. <span class="sig-name descname"><span class="n"><span class="pre">Py_T_UINT</span></span></span><a class="headerlink" href="#c.Py_T_UINT" title="Link to this definition">¶</a><br /></dt>
  811. <dd></dd></dl>
  812. </td>
  813. <td><p><span class="c-expr sig sig-inline c"><span class="kt">unsigned</span><span class="w"> </span><span class="kt">int</span></span></p></td>
  814. <td><p><a class="reference internal" href="../library/functions.html#int" title="int"><code class="xref py py-class docutils literal notranslate"><span class="pre">int</span></code></a></p></td>
  815. </tr>
  816. <tr class="row-odd"><td><dl class="c macro">
  817. <dt class="sig sig-object c" id="c.Py_T_USHORT">
  818. <span class="sig-name descname"><span class="n"><span class="pre">Py_T_USHORT</span></span></span><a class="headerlink" href="#c.Py_T_USHORT" title="Link to this definition">¶</a><br /></dt>
  819. <dd></dd></dl>
  820. </td>
  821. <td><p><span class="c-expr sig sig-inline c"><span class="kt">unsigned</span><span class="w"> </span><span class="kt">short</span></span></p></td>
  822. <td><p><a class="reference internal" href="../library/functions.html#int" title="int"><code class="xref py py-class docutils literal notranslate"><span class="pre">int</span></code></a></p></td>
  823. </tr>
  824. <tr class="row-even"><td><dl class="c macro">
  825. <dt class="sig sig-object c" id="c.Py_T_ULONG">
  826. <span class="sig-name descname"><span class="n"><span class="pre">Py_T_ULONG</span></span></span><a class="headerlink" href="#c.Py_T_ULONG" title="Link to this definition">¶</a><br /></dt>
  827. <dd></dd></dl>
  828. </td>
  829. <td><p><span class="c-expr sig sig-inline c"><span class="kt">unsigned</span><span class="w"> </span><span class="kt">long</span></span></p></td>
  830. <td><p><a class="reference internal" href="../library/functions.html#int" title="int"><code class="xref py py-class docutils literal notranslate"><span class="pre">int</span></code></a></p></td>
  831. </tr>
  832. <tr class="row-odd"><td><dl class="c macro">
  833. <dt class="sig sig-object c" id="c.Py_T_ULONGLONG">
  834. <span class="sig-name descname"><span class="n"><span class="pre">Py_T_ULONGLONG</span></span></span><a class="headerlink" href="#c.Py_T_ULONGLONG" title="Link to this definition">¶</a><br /></dt>
  835. <dd></dd></dl>
  836. </td>
  837. <td><p><span class="c-expr sig sig-inline c"><span class="kt">unsigned</span><span class="w"> </span><span class="kt">long</span><span class="w"> </span><span class="kt">long</span></span></p></td>
  838. <td><p><a class="reference internal" href="../library/functions.html#int" title="int"><code class="xref py py-class docutils literal notranslate"><span class="pre">int</span></code></a></p></td>
  839. </tr>
  840. <tr class="row-even"><td><dl class="c macro">
  841. <dt class="sig sig-object c" id="c.Py_T_PYSSIZET">
  842. <span class="sig-name descname"><span class="n"><span class="pre">Py_T_PYSSIZET</span></span></span><a class="headerlink" href="#c.Py_T_PYSSIZET" title="Link to this definition">¶</a><br /></dt>
  843. <dd></dd></dl>
  844. </td>
  845. <td><p><span class="c-expr sig sig-inline c"><a class="reference internal" href="intro.html#c.Py_ssize_t" title="Py_ssize_t"><span class="n">Py_ssize_t</span></a></span></p></td>
  846. <td><p><a class="reference internal" href="../library/functions.html#int" title="int"><code class="xref py py-class docutils literal notranslate"><span class="pre">int</span></code></a></p></td>
  847. </tr>
  848. <tr class="row-odd"><td><dl class="c macro">
  849. <dt class="sig sig-object c" id="c.Py_T_FLOAT">
  850. <span class="sig-name descname"><span class="n"><span class="pre">Py_T_FLOAT</span></span></span><a class="headerlink" href="#c.Py_T_FLOAT" title="Link to this definition">¶</a><br /></dt>
  851. <dd></dd></dl>
  852. </td>
  853. <td><p><span class="c-expr sig sig-inline c"><span class="kt">float</span></span></p></td>
  854. <td><p><a class="reference internal" href="../library/functions.html#float" title="float"><code class="xref py py-class docutils literal notranslate"><span class="pre">float</span></code></a></p></td>
  855. </tr>
  856. <tr class="row-even"><td><dl class="c macro">
  857. <dt class="sig sig-object c" id="c.Py_T_DOUBLE">
  858. <span class="sig-name descname"><span class="n"><span class="pre">Py_T_DOUBLE</span></span></span><a class="headerlink" href="#c.Py_T_DOUBLE" title="Link to this definition">¶</a><br /></dt>
  859. <dd></dd></dl>
  860. </td>
  861. <td><p><span class="c-expr sig sig-inline c"><span class="kt">double</span></span></p></td>
  862. <td><p><a class="reference internal" href="../library/functions.html#float" title="float"><code class="xref py py-class docutils literal notranslate"><span class="pre">float</span></code></a></p></td>
  863. </tr>
  864. <tr class="row-odd"><td><dl class="c macro">
  865. <dt class="sig sig-object c" id="c.Py_T_BOOL">
  866. <span class="sig-name descname"><span class="n"><span class="pre">Py_T_BOOL</span></span></span><a class="headerlink" href="#c.Py_T_BOOL" title="Link to this definition">¶</a><br /></dt>
  867. <dd></dd></dl>
  868. </td>
  869. <td><p><span class="c-expr sig sig-inline c"><span class="kt">char</span></span>
  870. (written as 0 or 1)</p></td>
  871. <td><p><a class="reference internal" href="../library/functions.html#bool" title="bool"><code class="xref py py-class docutils literal notranslate"><span class="pre">bool</span></code></a></p></td>
  872. </tr>
  873. <tr class="row-even"><td><dl class="c macro">
  874. <dt class="sig sig-object c" id="c.Py_T_STRING">
  875. <span class="sig-name descname"><span class="n"><span class="pre">Py_T_STRING</span></span></span><a class="headerlink" href="#c.Py_T_STRING" title="Link to this definition">¶</a><br /></dt>
  876. <dd></dd></dl>
  877. </td>
  878. <td><p><span class="c-expr sig sig-inline c"><span class="k">const</span><span class="w"> </span><span class="kt">char</span><span class="p">*</span></span> (*)</p></td>
  879. <td><p><a class="reference internal" href="../library/stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> (RO)</p></td>
  880. </tr>
  881. <tr class="row-odd"><td><dl class="c macro">
  882. <dt class="sig sig-object c" id="c.Py_T_STRING_INPLACE">
  883. <span class="sig-name descname"><span class="n"><span class="pre">Py_T_STRING_INPLACE</span></span></span><a class="headerlink" href="#c.Py_T_STRING_INPLACE" title="Link to this definition">¶</a><br /></dt>
  884. <dd></dd></dl>
  885. </td>
  886. <td><p><span class="c-expr sig sig-inline c"><span class="k">const</span><span class="w"> </span><span class="kt">char</span><span class="p">[</span><span class="p">]</span></span> (*)</p></td>
  887. <td><p><a class="reference internal" href="../library/stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> (RO)</p></td>
  888. </tr>
  889. <tr class="row-even"><td><dl class="c macro">
  890. <dt class="sig sig-object c" id="c.Py_T_CHAR">
  891. <span class="sig-name descname"><span class="n"><span class="pre">Py_T_CHAR</span></span></span><a class="headerlink" href="#c.Py_T_CHAR" title="Link to this definition">¶</a><br /></dt>
  892. <dd></dd></dl>
  893. </td>
  894. <td><p><span class="c-expr sig sig-inline c"><span class="kt">char</span></span> (0-127)</p></td>
  895. <td><p><a class="reference internal" href="../library/stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> (**)</p></td>
  896. </tr>
  897. <tr class="row-odd"><td><dl class="c macro">
  898. <dt class="sig sig-object c" id="c.Py_T_OBJECT_EX">
  899. <span class="sig-name descname"><span class="n"><span class="pre">Py_T_OBJECT_EX</span></span></span><a class="headerlink" href="#c.Py_T_OBJECT_EX" title="Link to this definition">¶</a><br /></dt>
  900. <dd></dd></dl>
  901. </td>
  902. <td><p><span class="c-expr sig sig-inline c"><a class="reference internal" href="#c.PyObject" title="PyObject"><span class="n">PyObject</span></a><span class="p">*</span></span></p></td>
  903. <td><p><a class="reference internal" href="../library/functions.html#object" title="object"><code class="xref py py-class docutils literal notranslate"><span class="pre">object</span></code></a> (D)</p></td>
  904. </tr>
  905. </tbody>
  906. </table>
  907. <blockquote>
  908. <div><p>(*): Zero-terminated, UTF8-encoded C string.
  909. With <code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_T_STRING</span></code> the C representation is a pointer;
  910. with <code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_T_STRING_INPLACE</span></code> the string is stored directly
  911. in the structure.</p>
  912. <p>(**): String of length 1. Only ASCII is accepted.</p>
  913. <p>(RO): Implies <a class="reference internal" href="#c.Py_READONLY" title="Py_READONLY"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_READONLY</span></code></a>.</p>
  914. <p>(D): Can be deleted, in which case the pointer is set to <code class="docutils literal notranslate"><span class="pre">NULL</span></code>.
  915. Reading a <code class="docutils literal notranslate"><span class="pre">NULL</span></code> pointer raises <a class="reference internal" href="../library/exceptions.html#AttributeError" title="AttributeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">AttributeError</span></code></a>.</p>
  916. </div></blockquote>
  917. <div class="versionadded" id="index-4">
  918. <p><span class="versionmodified added">New in version 3.12: </span>In previous versions, the macros were only available with
  919. <code class="docutils literal notranslate"><span class="pre">#include</span> <span class="pre">&quot;structmember.h&quot;</span></code> and were named without the <code class="docutils literal notranslate"><span class="pre">Py_</span></code> prefix
  920. (e.g. as <code class="docutils literal notranslate"><span class="pre">T_INT</span></code>).
  921. The header is still available and contains the old names, along with
  922. the following deprecated types:</p>
  923. <dl class="c macro">
  924. <dt class="sig sig-object c" id="c.T_OBJECT">
  925. <span class="sig-name descname"><span class="n"><span class="pre">T_OBJECT</span></span></span><a class="headerlink" href="#c.T_OBJECT" title="Link to this definition">¶</a><br /></dt>
  926. <dd><p>Like <code class="docutils literal notranslate"><span class="pre">Py_T_OBJECT_EX</span></code>, but <code class="docutils literal notranslate"><span class="pre">NULL</span></code> is converted to <code class="docutils literal notranslate"><span class="pre">None</span></code>.
  927. This results in surprising behavior in Python: deleting the attribute
  928. effectively sets it to <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  929. </dd></dl>
  930. <dl class="c macro">
  931. <dt class="sig sig-object c" id="c.T_NONE">
  932. <span class="sig-name descname"><span class="n"><span class="pre">T_NONE</span></span></span><a class="headerlink" href="#c.T_NONE" title="Link to this definition">¶</a><br /></dt>
  933. <dd><p>Always <code class="docutils literal notranslate"><span class="pre">None</span></code>. Must be used with <a class="reference internal" href="#c.Py_READONLY" title="Py_READONLY"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_READONLY</span></code></a>.</p>
  934. </dd></dl>
  935. </div>
  936. </section>
  937. <section id="defining-getters-and-setters">
  938. <h3>Defining Getters and Setters<a class="headerlink" href="#defining-getters-and-setters" title="Link to this heading">¶</a></h3>
  939. <dl class="c type">
  940. <dt class="sig sig-object c" id="c.PyGetSetDef">
  941. <span class="k"><span class="pre">type</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyGetSetDef</span></span></span><a class="headerlink" href="#c.PyGetSetDef" title="Link to this definition">¶</a><br /></dt>
  942. <dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a> (including all members).</em><p>Structure to define property-like access for a type. See also description of
  943. the <a class="reference internal" href="typeobj.html#c.PyTypeObject.tp_getset" title="PyTypeObject.tp_getset"><code class="xref c c-member docutils literal notranslate"><span class="pre">PyTypeObject.tp_getset</span></code></a> slot.</p>
  944. <dl class="c member">
  945. <dt class="sig sig-object c" id="c.PyGetSetDef.name">
  946. <span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">name</span></span></span><a class="headerlink" href="#c.PyGetSetDef.name" title="Link to this definition">¶</a><br /></dt>
  947. <dd><p>attribute name</p>
  948. </dd></dl>
  949. <dl class="c member">
  950. <dt class="sig sig-object c" id="c.PyGetSetDef.get">
  951. <a class="reference internal" href="#c.getter" title="getter"><span class="n"><span class="pre">getter</span></span></a><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">get</span></span></span><a class="headerlink" href="#c.PyGetSetDef.get" title="Link to this definition">¶</a><br /></dt>
  952. <dd><p>C function to get the attribute.</p>
  953. </dd></dl>
  954. <dl class="c member">
  955. <dt class="sig sig-object c" id="c.PyGetSetDef.set">
  956. <a class="reference internal" href="#c.setter" title="setter"><span class="n"><span class="pre">setter</span></span></a><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">set</span></span></span><a class="headerlink" href="#c.PyGetSetDef.set" title="Link to this definition">¶</a><br /></dt>
  957. <dd><p>Optional C function to set or delete the attribute.
  958. If <code class="docutils literal notranslate"><span class="pre">NULL</span></code>, the attribute is read-only.</p>
  959. </dd></dl>
  960. <dl class="c member">
  961. <dt class="sig sig-object c" id="c.PyGetSetDef.doc">
  962. <span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">doc</span></span></span><a class="headerlink" href="#c.PyGetSetDef.doc" title="Link to this definition">¶</a><br /></dt>
  963. <dd><p>optional docstring</p>
  964. </dd></dl>
  965. <dl class="c member">
  966. <dt class="sig sig-object c" id="c.PyGetSetDef.closure">
  967. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">closure</span></span></span><a class="headerlink" href="#c.PyGetSetDef.closure" title="Link to this definition">¶</a><br /></dt>
  968. <dd><p>Optional user data pointer, providing additional data for getter and setter.</p>
  969. </dd></dl>
  970. </dd></dl>
  971. <dl class="c type">
  972. <dt class="sig sig-object c" id="c.getter">
  973. <span class="k"><span class="pre">typedef</span></span><span class="w"> </span><a class="reference internal" href="#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="p"><span class="pre">(</span></span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">getter</span></span></span><span class="p"><span class="pre">)</span></span><span class="p"><span class="pre">(</span></span><a class="reference internal" href="#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="p"><span class="pre">*</span></span><span class="p"><span class="pre">,</span></span><span class="w"> </span><span class="kt"><span class="pre">void</span></span><span class="p"><span class="pre">*</span></span><span class="p"><span class="pre">)</span></span><a class="headerlink" href="#c.getter" title="Link to this definition">¶</a><br /></dt>
  974. <dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a>.</em><p>The <code class="docutils literal notranslate"><span class="pre">get</span></code> function takes one <span class="c-expr sig sig-inline c"><a class="reference internal" href="#c.PyObject" title="PyObject"><span class="n">PyObject</span></a><span class="p">*</span></span> parameter (the
  975. instance) and a user data pointer (the associated <code class="docutils literal notranslate"><span class="pre">closure</span></code>):</p>
  976. <p>It should return a new reference on success or <code class="docutils literal notranslate"><span class="pre">NULL</span></code> with a set exception
  977. on failure.</p>
  978. </dd></dl>
  979. <dl class="c type">
  980. <dt class="sig sig-object c" id="c.setter">
  981. <span class="k"><span class="pre">typedef</span></span><span class="w"> </span><span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="p"><span class="pre">(</span></span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">setter</span></span></span><span class="p"><span class="pre">)</span></span><span class="p"><span class="pre">(</span></span><a class="reference internal" href="#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="p"><span class="pre">*</span></span><span class="p"><span class="pre">,</span></span><span class="w"> </span><a class="reference internal" href="#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="p"><span class="pre">*</span></span><span class="p"><span class="pre">,</span></span><span class="w"> </span><span class="kt"><span class="pre">void</span></span><span class="p"><span class="pre">*</span></span><span class="p"><span class="pre">)</span></span><a class="headerlink" href="#c.setter" title="Link to this definition">¶</a><br /></dt>
  982. <dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a>.</em><p><code class="docutils literal notranslate"><span class="pre">set</span></code> functions take two <span class="c-expr sig sig-inline c"><a class="reference internal" href="#c.PyObject" title="PyObject"><span class="n">PyObject</span></a><span class="p">*</span></span> parameters (the instance and
  983. the value to be set) and a user data pointer (the associated <code class="docutils literal notranslate"><span class="pre">closure</span></code>):</p>
  984. <p>In case the attribute should be deleted the second parameter is <code class="docutils literal notranslate"><span class="pre">NULL</span></code>.
  985. Should return <code class="docutils literal notranslate"><span class="pre">0</span></code> on success or <code class="docutils literal notranslate"><span class="pre">-1</span></code> with a set exception on failure.</p>
  986. </dd></dl>
  987. </section>
  988. </section>
  989. </section>
  990. <div class="clearer"></div>
  991. </div>
  992. </div>
  993. </div>
  994. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  995. <div class="sphinxsidebarwrapper">
  996. <div>
  997. <h3><a href="../contents.html">Table of Contents</a></h3>
  998. <ul>
  999. <li><a class="reference internal" href="#">Common Object Structures</a><ul>
  1000. <li><a class="reference internal" href="#base-object-types-and-macros">Base object types and macros</a></li>
  1001. <li><a class="reference internal" href="#implementing-functions-and-methods">Implementing functions and methods</a></li>
  1002. <li><a class="reference internal" href="#accessing-attributes-of-extension-types">Accessing attributes of extension types</a><ul>
  1003. <li><a class="reference internal" href="#member-flags">Member flags</a></li>
  1004. <li><a class="reference internal" href="#member-types">Member types</a></li>
  1005. <li><a class="reference internal" href="#defining-getters-and-setters">Defining Getters and Setters</a></li>
  1006. </ul>
  1007. </li>
  1008. </ul>
  1009. </li>
  1010. </ul>
  1011. </div>
  1012. <div>
  1013. <h4>Previous topic</h4>
  1014. <p class="topless"><a href="allocation.html"
  1015. title="previous chapter">Allocating Objects on the Heap</a></p>
  1016. </div>
  1017. <div>
  1018. <h4>Next topic</h4>
  1019. <p class="topless"><a href="typeobj.html"
  1020. title="next chapter">Type Objects</a></p>
  1021. </div>
  1022. <div role="note" aria-label="source link">
  1023. <h3>This Page</h3>
  1024. <ul class="this-page-menu">
  1025. <li><a href="../bugs.html">Report a Bug</a></li>
  1026. <li>
  1027. <a href="https://github.com/python/cpython/blob/main/Doc/c-api/structures.rst"
  1028. rel="nofollow">Show Source
  1029. </a>
  1030. </li>
  1031. </ul>
  1032. </div>
  1033. </div>
  1034. <div id="sidebarbutton" title="Collapse sidebar">
  1035. <span>«</span>
  1036. </div>
  1037. </div>
  1038. <div class="clearer"></div>
  1039. </div>
  1040. <div class="related" role="navigation" aria-label="related navigation">
  1041. <h3>Navigation</h3>
  1042. <ul>
  1043. <li class="right" style="margin-right: 10px">
  1044. <a href="../genindex.html" title="General Index"
  1045. >index</a></li>
  1046. <li class="right" >
  1047. <a href="../py-modindex.html" title="Python Module Index"
  1048. >modules</a> |</li>
  1049. <li class="right" >
  1050. <a href="typeobj.html" title="Type Objects"
  1051. >next</a> |</li>
  1052. <li class="right" >
  1053. <a href="allocation.html" title="Allocating Objects on the Heap"
  1054. >previous</a> |</li>
  1055. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  1056. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  1057. <li class="switchers">
  1058. <div class="language_switcher_placeholder"></div>
  1059. <div class="version_switcher_placeholder"></div>
  1060. </li>
  1061. <li>
  1062. </li>
  1063. <li id="cpython-language-and-version">
  1064. <a href="../index.html">3.12.3 Documentation</a> &#187;
  1065. </li>
  1066. <li class="nav-item nav-item-1"><a href="index.html" >Python/C API Reference Manual</a> &#187;</li>
  1067. <li class="nav-item nav-item-2"><a href="objimpl.html" >Object Implementation Support</a> &#187;</li>
  1068. <li class="nav-item nav-item-this"><a href="">Common Object Structures</a></li>
  1069. <li class="right">
  1070. <div class="inline-search" role="search">
  1071. <form class="inline-search" action="../search.html" method="get">
  1072. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  1073. <input type="submit" value="Go" />
  1074. </form>
  1075. </div>
  1076. |
  1077. </li>
  1078. <li class="right">
  1079. <label class="theme-selector-label">
  1080. Theme
  1081. <select class="theme-selector" oninput="activateTheme(this.value)">
  1082. <option value="auto" selected>Auto</option>
  1083. <option value="light">Light</option>
  1084. <option value="dark">Dark</option>
  1085. </select>
  1086. </label> |</li>
  1087. </ul>
  1088. </div>
  1089. <div class="footer">
  1090. &copy;
  1091. <a href="../copyright.html">
  1092. Copyright
  1093. </a>
  1094. 2001-2024, Python Software Foundation.
  1095. <br />
  1096. This page is licensed under the Python Software Foundation License Version 2.
  1097. <br />
  1098. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  1099. <br />
  1100. See <a href="/license.html">History and License</a> for more information.<br />
  1101. <br />
  1102. The Python Software Foundation is a non-profit corporation.
  1103. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  1104. <br />
  1105. <br />
  1106. Last updated on Apr 09, 2024 (13:47 UTC).
  1107. <a href="/bugs.html">Found a bug</a>?
  1108. <br />
  1109. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  1110. </div>
  1111. </body>
  1112. </html>
上海开阖软件有限公司 沪ICP备12045867号-1