gooderp18绿色标准版
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

772 linhas
94KB

  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="Object Protocol" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/c-api/object.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
  11. <meta property="og:image:alt" content="Python documentation" />
  12. <meta property="og:image:width" content="200" />
  13. <meta property="og:image:height" content="200" />
  14. <meta name="theme-color" content="#3776ab" />
  15. <title>Object Protocol &#8212; Python 3.12.3 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  16. <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=80d5e7a1" />
  17. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=bb723527" />
  18. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=b20cc3f5" />
  19. <script src="../_static/documentation_options.js?v=2c828074"></script>
  20. <script src="../_static/doctools.js?v=888ff710"></script>
  21. <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
  22. <script src="../_static/sidebar.js"></script>
  23. <link rel="search" type="application/opensearchdescription+xml"
  24. title="Search within Python 3.12.3 documentation"
  25. href="../_static/opensearch.xml"/>
  26. <link rel="author" title="About these documents" href="../about.html" />
  27. <link rel="index" title="Index" href="../genindex.html" />
  28. <link rel="search" title="Search" href="../search.html" />
  29. <link rel="copyright" title="Copyright" href="../copyright.html" />
  30. <link rel="next" title="Call Protocol" href="call.html" />
  31. <link rel="prev" title="Abstract Objects Layer" href="abstract.html" />
  32. <link rel="canonical" href="https://docs.python.org/3/c-api/object.html" />
  33. <style>
  34. @media only screen {
  35. table.full-width-table {
  36. width: 100%;
  37. }
  38. }
  39. </style>
  40. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  41. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  42. <script type="text/javascript" src="../_static/copybutton.js"></script>
  43. <script type="text/javascript" src="../_static/menu.js"></script>
  44. <script type="text/javascript" src="../_static/search-focus.js"></script>
  45. <script type="text/javascript" src="../_static/themetoggle.js"></script>
  46. </head>
  47. <body>
  48. <div class="mobile-nav">
  49. <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
  50. aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
  51. <nav class="nav-content" role="navigation">
  52. <label for="menuToggler" class="toggler__label">
  53. <span></span>
  54. </label>
  55. <span class="nav-items-wrapper">
  56. <a href="https://www.python.org/" class="nav-logo">
  57. <img src="../_static/py.svg" alt="Python logo"/>
  58. </a>
  59. <span class="version_switcher_placeholder"></span>
  60. <form role="search" class="search" action="../search.html" method="get">
  61. <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
  62. <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>
  63. </svg>
  64. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  65. <input type="submit" value="Go"/>
  66. </form>
  67. </span>
  68. </nav>
  69. <div class="menu-wrapper">
  70. <nav class="menu" role="navigation" aria-label="main navigation">
  71. <div class="language_switcher_placeholder"></div>
  72. <label class="theme-selector-label">
  73. Theme
  74. <select class="theme-selector" oninput="activateTheme(this.value)">
  75. <option value="auto" selected>Auto</option>
  76. <option value="light">Light</option>
  77. <option value="dark">Dark</option>
  78. </select>
  79. </label>
  80. <div>
  81. <h4>Previous topic</h4>
  82. <p class="topless"><a href="abstract.html"
  83. title="previous chapter">Abstract Objects Layer</a></p>
  84. </div>
  85. <div>
  86. <h4>Next topic</h4>
  87. <p class="topless"><a href="call.html"
  88. title="next chapter">Call Protocol</a></p>
  89. </div>
  90. <div role="note" aria-label="source link">
  91. <h3>This Page</h3>
  92. <ul class="this-page-menu">
  93. <li><a href="../bugs.html">Report a Bug</a></li>
  94. <li>
  95. <a href="https://github.com/python/cpython/blob/main/Doc/c-api/object.rst"
  96. rel="nofollow">Show Source
  97. </a>
  98. </li>
  99. </ul>
  100. </div>
  101. </nav>
  102. </div>
  103. </div>
  104. <div class="related" role="navigation" aria-label="related navigation">
  105. <h3>Navigation</h3>
  106. <ul>
  107. <li class="right" style="margin-right: 10px">
  108. <a href="../genindex.html" title="General Index"
  109. accesskey="I">index</a></li>
  110. <li class="right" >
  111. <a href="../py-modindex.html" title="Python Module Index"
  112. >modules</a> |</li>
  113. <li class="right" >
  114. <a href="call.html" title="Call Protocol"
  115. accesskey="N">next</a> |</li>
  116. <li class="right" >
  117. <a href="abstract.html" title="Abstract Objects Layer"
  118. accesskey="P">previous</a> |</li>
  119. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  120. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  121. <li class="switchers">
  122. <div class="language_switcher_placeholder"></div>
  123. <div class="version_switcher_placeholder"></div>
  124. </li>
  125. <li>
  126. </li>
  127. <li id="cpython-language-and-version">
  128. <a href="../index.html">3.12.3 Documentation</a> &#187;
  129. </li>
  130. <li class="nav-item nav-item-1"><a href="index.html" >Python/C API Reference Manual</a> &#187;</li>
  131. <li class="nav-item nav-item-2"><a href="abstract.html" accesskey="U">Abstract Objects Layer</a> &#187;</li>
  132. <li class="nav-item nav-item-this"><a href="">Object Protocol</a></li>
  133. <li class="right">
  134. <div class="inline-search" role="search">
  135. <form class="inline-search" action="../search.html" method="get">
  136. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  137. <input type="submit" value="Go" />
  138. </form>
  139. </div>
  140. |
  141. </li>
  142. <li class="right">
  143. <label class="theme-selector-label">
  144. Theme
  145. <select class="theme-selector" oninput="activateTheme(this.value)">
  146. <option value="auto" selected>Auto</option>
  147. <option value="light">Light</option>
  148. <option value="dark">Dark</option>
  149. </select>
  150. </label> |</li>
  151. </ul>
  152. </div>
  153. <div class="document">
  154. <div class="documentwrapper">
  155. <div class="bodywrapper">
  156. <div class="body" role="main">
  157. <section id="object-protocol">
  158. <span id="object"></span><h1>Object Protocol<a class="headerlink" href="#object-protocol" title="Link to this heading">¶</a></h1>
  159. <dl class="c var">
  160. <dt class="sig sig-object c" id="c.Py_NotImplemented">
  161. <a class="reference internal" href="structures.html#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">Py_NotImplemented</span></span></span><a class="headerlink" href="#c.Py_NotImplemented" title="Link to this definition">¶</a><br /></dt>
  162. <dd><p>The <code class="docutils literal notranslate"><span class="pre">NotImplemented</span></code> singleton, used to signal that an operation is
  163. not implemented for the given type combination.</p>
  164. </dd></dl>
  165. <dl class="c macro">
  166. <dt class="sig sig-object c" id="c.Py_RETURN_NOTIMPLEMENTED">
  167. <span class="sig-name descname"><span class="n"><span class="pre">Py_RETURN_NOTIMPLEMENTED</span></span></span><a class="headerlink" href="#c.Py_RETURN_NOTIMPLEMENTED" title="Link to this definition">¶</a><br /></dt>
  168. <dd><p>Properly handle returning <a class="reference internal" href="#c.Py_NotImplemented" title="Py_NotImplemented"><code class="xref c c-data docutils literal notranslate"><span class="pre">Py_NotImplemented</span></code></a> from within a C
  169. function (that is, create a new <a class="reference internal" href="../glossary.html#term-strong-reference"><span class="xref std std-term">strong reference</span></a>
  170. to NotImplemented and return it).</p>
  171. </dd></dl>
  172. <dl class="c macro">
  173. <dt class="sig sig-object c" id="c.Py_PRINT_RAW">
  174. <span class="sig-name descname"><span class="n"><span class="pre">Py_PRINT_RAW</span></span></span><a class="headerlink" href="#c.Py_PRINT_RAW" title="Link to this definition">¶</a><br /></dt>
  175. <dd><p>Flag to be used with multiple functions that print the object (like
  176. <a class="reference internal" href="#c.PyObject_Print" title="PyObject_Print"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_Print()</span></code></a> and <a class="reference internal" href="file.html#c.PyFile_WriteObject" title="PyFile_WriteObject"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyFile_WriteObject()</span></code></a>).
  177. If passed, these function would use the <a class="reference internal" href="../library/stdtypes.html#str" title="str"><code class="xref py py-func docutils literal notranslate"><span class="pre">str()</span></code></a> of the object
  178. instead of the <a class="reference internal" href="../library/functions.html#repr" title="repr"><code class="xref py py-func docutils literal notranslate"><span class="pre">repr()</span></code></a>.</p>
  179. </dd></dl>
  180. <dl class="c function">
  181. <dt class="sig sig-object c" id="c.PyObject_Print">
  182. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyObject_Print</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#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="n"><span class="pre">FILE</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">fp</span></span>, <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="n"><span class="pre">flags</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyObject_Print" title="Link to this definition">¶</a><br /></dt>
  183. <dd><p>Print an object <em>o</em>, on file <em>fp</em>. Returns <code class="docutils literal notranslate"><span class="pre">-1</span></code> on error. The flags argument
  184. is used to enable certain printing options. The only option currently supported
  185. is <a class="reference internal" href="#c.Py_PRINT_RAW" title="Py_PRINT_RAW"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_PRINT_RAW</span></code></a>; if given, the <a class="reference internal" href="../library/stdtypes.html#str" title="str"><code class="xref py py-func docutils literal notranslate"><span class="pre">str()</span></code></a> of the object is written
  186. instead of the <a class="reference internal" href="../library/functions.html#repr" title="repr"><code class="xref py py-func docutils literal notranslate"><span class="pre">repr()</span></code></a>.</p>
  187. </dd></dl>
  188. <dl class="c function">
  189. <dt class="sig sig-object c" id="c.PyObject_HasAttr">
  190. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyObject_HasAttr</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#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="structures.html#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">attr_name</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyObject_HasAttr" 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">Stable ABI</span></a>.</em><p>Returns <code class="docutils literal notranslate"><span class="pre">1</span></code> if <em>o</em> has the attribute <em>attr_name</em>, and <code class="docutils literal notranslate"><span class="pre">0</span></code> otherwise. This
  192. is equivalent to the Python expression <code class="docutils literal notranslate"><span class="pre">hasattr(o,</span> <span class="pre">attr_name)</span></code>. This function
  193. always succeeds.</p>
  194. <div class="admonition note">
  195. <p class="admonition-title">Note</p>
  196. <p>Exceptions that occur when this calls <a class="reference internal" href="../reference/datamodel.html#object.__getattr__" title="object.__getattr__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__getattr__()</span></code></a> and
  197. <a class="reference internal" href="../reference/datamodel.html#object.__getattribute__" title="object.__getattribute__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__getattribute__()</span></code></a> methods are silently ignored.
  198. For proper error handling, use <a class="reference internal" href="#c.PyObject_GetAttr" title="PyObject_GetAttr"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_GetAttr()</span></code></a> instead.</p>
  199. </div>
  200. </dd></dl>
  201. <dl class="c function">
  202. <dt class="sig sig-object c" id="c.PyObject_HasAttrString">
  203. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyObject_HasAttrString</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#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="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">attr_name</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyObject_HasAttrString" title="Link to this definition">¶</a><br /></dt>
  204. <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>This is the same as <a class="reference internal" href="#c.PyObject_HasAttr" title="PyObject_HasAttr"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_HasAttr()</span></code></a>, but <em>attr_name</em> is
  205. specified as a <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> UTF-8 encoded bytes string,
  206. rather than a <span class="c-expr sig sig-inline c"><a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n">PyObject</span></a><span class="p">*</span></span>.</p>
  207. <div class="admonition note">
  208. <p class="admonition-title">Note</p>
  209. <p>Exceptions that occur when this calls <a class="reference internal" href="../reference/datamodel.html#object.__getattr__" title="object.__getattr__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__getattr__()</span></code></a> and
  210. <a class="reference internal" href="../reference/datamodel.html#object.__getattribute__" title="object.__getattribute__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__getattribute__()</span></code></a> methods or while creating the temporary
  211. <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> object are silently ignored.
  212. For proper error handling, use <a class="reference internal" href="#c.PyObject_GetAttrString" title="PyObject_GetAttrString"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_GetAttrString()</span></code></a> instead.</p>
  213. </div>
  214. </dd></dl>
  215. <dl class="c function">
  216. <dt class="sig sig-object c" id="c.PyObject_GetAttr">
  217. <a class="reference internal" href="structures.html#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">PyObject_GetAttr</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#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="structures.html#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">attr_name</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyObject_GetAttr" title="Link to this definition">¶</a><br /></dt>
  218. <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>Retrieve an attribute named <em>attr_name</em> from object <em>o</em>. Returns the attribute
  219. value on success, or <code class="docutils literal notranslate"><span class="pre">NULL</span></code> on failure. This is the equivalent of the Python
  220. expression <code class="docutils literal notranslate"><span class="pre">o.attr_name</span></code>.</p>
  221. </dd></dl>
  222. <dl class="c function">
  223. <dt class="sig sig-object c" id="c.PyObject_GetAttrString">
  224. <a class="reference internal" href="structures.html#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">PyObject_GetAttrString</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#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="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">attr_name</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyObject_GetAttrString" title="Link to this definition">¶</a><br /></dt>
  225. <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>This is the same as <a class="reference internal" href="#c.PyObject_GetAttr" title="PyObject_GetAttr"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_GetAttr()</span></code></a>, but <em>attr_name</em> is
  226. specified as a <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> UTF-8 encoded bytes string,
  227. rather than a <span class="c-expr sig sig-inline c"><a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n">PyObject</span></a><span class="p">*</span></span>.</p>
  228. </dd></dl>
  229. <dl class="c function">
  230. <dt class="sig sig-object c" id="c.PyObject_GenericGetAttr">
  231. <a class="reference internal" href="structures.html#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">PyObject_GenericGetAttr</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#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="structures.html#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">name</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyObject_GenericGetAttr" title="Link to this definition">¶</a><br /></dt>
  232. <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>Generic attribute getter function that is meant to be put into a type
  233. object’s <code class="docutils literal notranslate"><span class="pre">tp_getattro</span></code> slot. It looks for a descriptor in the dictionary
  234. of classes in the object’s MRO as well as an attribute in the object’s
  235. <a class="reference internal" href="../library/stdtypes.html#object.__dict__" title="object.__dict__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__dict__</span></code></a> (if present). As outlined in <a class="reference internal" href="../reference/datamodel.html#descriptors"><span class="std std-ref">Implementing Descriptors</span></a>,
  236. data descriptors take preference over instance attributes, while non-data
  237. descriptors don’t. Otherwise, an <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> is raised.</p>
  238. </dd></dl>
  239. <dl class="c function">
  240. <dt class="sig sig-object c" id="c.PyObject_SetAttr">
  241. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyObject_SetAttr</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#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="structures.html#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">attr_name</span></span>, <a class="reference internal" href="structures.html#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">v</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyObject_SetAttr" title="Link to this definition">¶</a><br /></dt>
  242. <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 the value of the attribute named <em>attr_name</em>, for object <em>o</em>, to the value
  243. <em>v</em>. Raise an exception and return <code class="docutils literal notranslate"><span class="pre">-1</span></code> on failure;
  244. return <code class="docutils literal notranslate"><span class="pre">0</span></code> on success. This is the equivalent of the Python statement
  245. <code class="docutils literal notranslate"><span class="pre">o.attr_name</span> <span class="pre">=</span> <span class="pre">v</span></code>.</p>
  246. <p>If <em>v</em> is <code class="docutils literal notranslate"><span class="pre">NULL</span></code>, the attribute is deleted. This behaviour is deprecated
  247. in favour of using <a class="reference internal" href="#c.PyObject_DelAttr" title="PyObject_DelAttr"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_DelAttr()</span></code></a>, but there are currently no
  248. plans to remove it.</p>
  249. </dd></dl>
  250. <dl class="c function">
  251. <dt class="sig sig-object c" id="c.PyObject_SetAttrString">
  252. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyObject_SetAttrString</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#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="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">attr_name</span></span>, <a class="reference internal" href="structures.html#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">v</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyObject_SetAttrString" title="Link to this definition">¶</a><br /></dt>
  253. <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>This is the same as <a class="reference internal" href="#c.PyObject_SetAttr" title="PyObject_SetAttr"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_SetAttr()</span></code></a>, but <em>attr_name</em> is
  254. specified as a <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> UTF-8 encoded bytes string,
  255. rather than a <span class="c-expr sig sig-inline c"><a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n">PyObject</span></a><span class="p">*</span></span>.</p>
  256. <p>If <em>v</em> is <code class="docutils literal notranslate"><span class="pre">NULL</span></code>, the attribute is deleted, but this feature is
  257. deprecated in favour of using <a class="reference internal" href="#c.PyObject_DelAttrString" title="PyObject_DelAttrString"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_DelAttrString()</span></code></a>.</p>
  258. </dd></dl>
  259. <dl class="c function">
  260. <dt class="sig sig-object c" id="c.PyObject_GenericSetAttr">
  261. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyObject_GenericSetAttr</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#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="structures.html#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">name</span></span>, <a class="reference internal" href="structures.html#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">value</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyObject_GenericSetAttr" title="Link to this definition">¶</a><br /></dt>
  262. <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>Generic attribute setter and deleter function that is meant
  263. to be put into a type object’s <a class="reference internal" href="typeobj.html#c.PyTypeObject.tp_setattro" title="PyTypeObject.tp_setattro"><code class="xref c c-member docutils literal notranslate"><span class="pre">tp_setattro</span></code></a>
  264. slot. It looks for a data descriptor in the
  265. dictionary of classes in the object’s MRO, and if found it takes preference
  266. over setting or deleting the attribute in the instance dictionary. Otherwise, the
  267. attribute is set or deleted in the object’s <a class="reference internal" href="../library/stdtypes.html#object.__dict__" title="object.__dict__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__dict__</span></code></a> (if present).
  268. On success, <code class="docutils literal notranslate"><span class="pre">0</span></code> is returned, otherwise an <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>
  269. is raised and <code class="docutils literal notranslate"><span class="pre">-1</span></code> is returned.</p>
  270. </dd></dl>
  271. <dl class="c function">
  272. <dt class="sig sig-object c" id="c.PyObject_DelAttr">
  273. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyObject_DelAttr</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#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="structures.html#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">attr_name</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyObject_DelAttr" title="Link to this definition">¶</a><br /></dt>
  274. <dd><p>Delete attribute named <em>attr_name</em>, for object <em>o</em>. Returns <code class="docutils literal notranslate"><span class="pre">-1</span></code> on failure.
  275. This is the equivalent of the Python statement <code class="docutils literal notranslate"><span class="pre">del</span> <span class="pre">o.attr_name</span></code>.</p>
  276. </dd></dl>
  277. <dl class="c function">
  278. <dt class="sig sig-object c" id="c.PyObject_DelAttrString">
  279. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyObject_DelAttrString</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#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="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">attr_name</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyObject_DelAttrString" title="Link to this definition">¶</a><br /></dt>
  280. <dd><p>This is the same as <a class="reference internal" href="#c.PyObject_DelAttr" title="PyObject_DelAttr"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_DelAttr()</span></code></a>, but <em>attr_name</em> is
  281. specified as a <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> UTF-8 encoded bytes string,
  282. rather than a <span class="c-expr sig sig-inline c"><a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n">PyObject</span></a><span class="p">*</span></span>.</p>
  283. </dd></dl>
  284. <dl class="c function">
  285. <dt class="sig sig-object c" id="c.PyObject_GenericGetDict">
  286. <a class="reference internal" href="structures.html#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">PyObject_GenericGetDict</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#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="kt"><span class="pre">void</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">context</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyObject_GenericGetDict" title="Link to this definition">¶</a><br /></dt>
  287. <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.10.</em><p>A generic implementation for the getter of a <code class="docutils literal notranslate"><span class="pre">__dict__</span></code> descriptor. It
  288. creates the dictionary if necessary.</p>
  289. <p>This function may also be called to get the <a class="reference internal" href="../library/stdtypes.html#object.__dict__" title="object.__dict__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__dict__</span></code></a>
  290. of the object <em>o</em>. Pass <code class="docutils literal notranslate"><span class="pre">NULL</span></code> for <em>context</em> when calling it.
  291. Since this function may need to allocate memory for the
  292. dictionary, it may be more efficient to call <a class="reference internal" href="#c.PyObject_GetAttr" title="PyObject_GetAttr"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_GetAttr()</span></code></a>
  293. when accessing an attribute on the object.</p>
  294. <p>On failure, returns <code class="docutils literal notranslate"><span class="pre">NULL</span></code> with an exception set.</p>
  295. <div class="versionadded">
  296. <p><span class="versionmodified added">New in version 3.3.</span></p>
  297. </div>
  298. </dd></dl>
  299. <dl class="c function">
  300. <dt class="sig sig-object c" id="c.PyObject_GenericSetDict">
  301. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyObject_GenericSetDict</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#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="structures.html#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">value</span></span>, <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">context</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyObject_GenericSetDict" title="Link to this definition">¶</a><br /></dt>
  302. <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.7.</em><p>A generic implementation for the setter of a <code class="docutils literal notranslate"><span class="pre">__dict__</span></code> descriptor. This
  303. implementation does not allow the dictionary to be deleted.</p>
  304. <div class="versionadded">
  305. <p><span class="versionmodified added">New in version 3.3.</span></p>
  306. </div>
  307. </dd></dl>
  308. <dl class="c function">
  309. <dt class="sig sig-object c" id="c._PyObject_GetDictPtr">
  310. <a class="reference internal" href="structures.html#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="sig-name descname"><span class="n"><span class="pre">_PyObject_GetDictPtr</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#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">obj</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c._PyObject_GetDictPtr" title="Link to this definition">¶</a><br /></dt>
  311. <dd><p>Return a pointer to <a class="reference internal" href="../library/stdtypes.html#object.__dict__" title="object.__dict__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__dict__</span></code></a> of the object <em>obj</em>.
  312. If there is no <code class="docutils literal notranslate"><span class="pre">__dict__</span></code>, return <code class="docutils literal notranslate"><span class="pre">NULL</span></code> without setting an exception.</p>
  313. <p>This function may need to allocate memory for the
  314. dictionary, so it may be more efficient to call <a class="reference internal" href="#c.PyObject_GetAttr" title="PyObject_GetAttr"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_GetAttr()</span></code></a>
  315. when accessing an attribute on the object.</p>
  316. </dd></dl>
  317. <dl class="c function">
  318. <dt class="sig sig-object c" id="c.PyObject_RichCompare">
  319. <a class="reference internal" href="structures.html#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">PyObject_RichCompare</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#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">o1</span></span>, <a class="reference internal" href="structures.html#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">o2</span></span>, <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="n"><span class="pre">opid</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyObject_RichCompare" title="Link to this definition">¶</a><br /></dt>
  320. <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>Compare the values of <em>o1</em> and <em>o2</em> using the operation specified by <em>opid</em>,
  321. which must be one of <a class="reference internal" href="typeobj.html#c.Py_LT" title="Py_LT"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_LT</span></code></a>, <a class="reference internal" href="typeobj.html#c.Py_LE" title="Py_LE"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_LE</span></code></a>, <a class="reference internal" href="typeobj.html#c.Py_EQ" title="Py_EQ"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_EQ</span></code></a>,
  322. <a class="reference internal" href="typeobj.html#c.Py_NE" title="Py_NE"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_NE</span></code></a>, <a class="reference internal" href="typeobj.html#c.Py_GT" title="Py_GT"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_GT</span></code></a>, or <a class="reference internal" href="typeobj.html#c.Py_GE" title="Py_GE"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_GE</span></code></a>, corresponding to <code class="docutils literal notranslate"><span class="pre">&lt;</span></code>,
  323. <code class="docutils literal notranslate"><span class="pre">&lt;=</span></code>, <code class="docutils literal notranslate"><span class="pre">==</span></code>, <code class="docutils literal notranslate"><span class="pre">!=</span></code>, <code class="docutils literal notranslate"><span class="pre">&gt;</span></code>, or <code class="docutils literal notranslate"><span class="pre">&gt;=</span></code> respectively. This is the equivalent of
  324. the Python expression <code class="docutils literal notranslate"><span class="pre">o1</span> <span class="pre">op</span> <span class="pre">o2</span></code>, where <code class="docutils literal notranslate"><span class="pre">op</span></code> is the operator corresponding
  325. to <em>opid</em>. Returns the value of the comparison on success, or <code class="docutils literal notranslate"><span class="pre">NULL</span></code> on failure.</p>
  326. </dd></dl>
  327. <dl class="c function">
  328. <dt class="sig sig-object c" id="c.PyObject_RichCompareBool">
  329. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyObject_RichCompareBool</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#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">o1</span></span>, <a class="reference internal" href="structures.html#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">o2</span></span>, <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="n"><span class="pre">opid</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyObject_RichCompareBool" title="Link to this definition">¶</a><br /></dt>
  330. <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>Compare the values of <em>o1</em> and <em>o2</em> using the operation specified by <em>opid</em>,
  331. like <a class="reference internal" href="#c.PyObject_RichCompare" title="PyObject_RichCompare"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_RichCompare()</span></code></a>, but returns <code class="docutils literal notranslate"><span class="pre">-1</span></code> on error, <code class="docutils literal notranslate"><span class="pre">0</span></code> if
  332. the result is false, <code class="docutils literal notranslate"><span class="pre">1</span></code> otherwise.</p>
  333. </dd></dl>
  334. <div class="admonition note">
  335. <p class="admonition-title">Note</p>
  336. <p>If <em>o1</em> and <em>o2</em> are the same object, <a class="reference internal" href="#c.PyObject_RichCompareBool" title="PyObject_RichCompareBool"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_RichCompareBool()</span></code></a>
  337. will always return <code class="docutils literal notranslate"><span class="pre">1</span></code> for <a class="reference internal" href="typeobj.html#c.Py_EQ" title="Py_EQ"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_EQ</span></code></a> and <code class="docutils literal notranslate"><span class="pre">0</span></code> for <a class="reference internal" href="typeobj.html#c.Py_NE" title="Py_NE"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_NE</span></code></a>.</p>
  338. </div>
  339. <dl class="c function">
  340. <dt class="sig sig-object c" id="c.PyObject_Format">
  341. <a class="reference internal" href="structures.html#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">PyObject_Format</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#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">obj</span></span>, <a class="reference internal" href="structures.html#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">format_spec</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyObject_Format" title="Link to this definition">¶</a><br /></dt>
  342. <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>Format <em>obj</em> using <em>format_spec</em>. This is equivalent to the Python
  343. expression <code class="docutils literal notranslate"><span class="pre">format(obj,</span> <span class="pre">format_spec)</span></code>.</p>
  344. <p><em>format_spec</em> may be <code class="docutils literal notranslate"><span class="pre">NULL</span></code>. In this case the call is equivalent
  345. to <code class="docutils literal notranslate"><span class="pre">format(obj)</span></code>.
  346. Returns the formatted string on success, <code class="docutils literal notranslate"><span class="pre">NULL</span></code> on failure.</p>
  347. </dd></dl>
  348. <dl class="c function">
  349. <dt class="sig sig-object c" id="c.PyObject_Repr">
  350. <a class="reference internal" href="structures.html#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">PyObject_Repr</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#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.PyObject_Repr" title="Link to this definition">¶</a><br /></dt>
  351. <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 id="index-0">Compute a string representation of object <em>o</em>. Returns the string
  352. representation on success, <code class="docutils literal notranslate"><span class="pre">NULL</span></code> on failure. This is the equivalent of the
  353. Python expression <code class="docutils literal notranslate"><span class="pre">repr(o)</span></code>. Called by the <a class="reference internal" href="../library/functions.html#repr" title="repr"><code class="xref py py-func docutils literal notranslate"><span class="pre">repr()</span></code></a> built-in function.</p>
  354. <div class="versionchanged">
  355. <p><span class="versionmodified changed">Changed in version 3.4: </span>This function now includes a debug assertion to help ensure that it
  356. does not silently discard an active exception.</p>
  357. </div>
  358. </dd></dl>
  359. <dl class="c function">
  360. <dt class="sig sig-object c" id="c.PyObject_ASCII">
  361. <a class="reference internal" href="structures.html#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">PyObject_ASCII</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#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.PyObject_ASCII" title="Link to this definition">¶</a><br /></dt>
  362. <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 id="index-1">As <a class="reference internal" href="#c.PyObject_Repr" title="PyObject_Repr"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_Repr()</span></code></a>, compute a string representation of object <em>o</em>, but
  363. escape the non-ASCII characters in the string returned by
  364. <a class="reference internal" href="#c.PyObject_Repr" title="PyObject_Repr"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_Repr()</span></code></a> with <code class="docutils literal notranslate"><span class="pre">\x</span></code>, <code class="docutils literal notranslate"><span class="pre">\u</span></code> or <code class="docutils literal notranslate"><span class="pre">\U</span></code> escapes. This generates
  365. a string similar to that returned by <a class="reference internal" href="#c.PyObject_Repr" title="PyObject_Repr"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_Repr()</span></code></a> in Python 2.
  366. Called by the <a class="reference internal" href="../library/functions.html#ascii" title="ascii"><code class="xref py py-func docutils literal notranslate"><span class="pre">ascii()</span></code></a> built-in function.</p>
  367. <span class="target" id="index-2"></span></dd></dl>
  368. <dl class="c function">
  369. <dt class="sig sig-object c" id="c.PyObject_Str">
  370. <a class="reference internal" href="structures.html#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">PyObject_Str</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#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.PyObject_Str" title="Link to this definition">¶</a><br /></dt>
  371. <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>Compute a string representation of object <em>o</em>. Returns the string
  372. representation on success, <code class="docutils literal notranslate"><span class="pre">NULL</span></code> on failure. This is the equivalent of the
  373. Python expression <code class="docutils literal notranslate"><span class="pre">str(o)</span></code>. Called by the <a class="reference internal" href="../library/stdtypes.html#str" title="str"><code class="xref py py-func docutils literal notranslate"><span class="pre">str()</span></code></a> built-in function
  374. and, therefore, by the <a class="reference internal" href="../library/functions.html#print" title="print"><code class="xref py py-func docutils literal notranslate"><span class="pre">print()</span></code></a> function.</p>
  375. <div class="versionchanged">
  376. <p><span class="versionmodified changed">Changed in version 3.4: </span>This function now includes a debug assertion to help ensure that it
  377. does not silently discard an active exception.</p>
  378. </div>
  379. </dd></dl>
  380. <dl class="c function">
  381. <dt class="sig sig-object c" id="c.PyObject_Bytes">
  382. <a class="reference internal" href="structures.html#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">PyObject_Bytes</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#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.PyObject_Bytes" title="Link to this definition">¶</a><br /></dt>
  383. <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 id="index-3">Compute a bytes representation of object <em>o</em>. <code class="docutils literal notranslate"><span class="pre">NULL</span></code> is returned on
  384. failure and a bytes object on success. This is equivalent to the Python
  385. expression <code class="docutils literal notranslate"><span class="pre">bytes(o)</span></code>, when <em>o</em> is not an integer. Unlike <code class="docutils literal notranslate"><span class="pre">bytes(o)</span></code>,
  386. a TypeError is raised when <em>o</em> is an integer instead of a zero-initialized
  387. bytes object.</p>
  388. </dd></dl>
  389. <dl class="c function">
  390. <dt class="sig sig-object c" id="c.PyObject_IsSubclass">
  391. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyObject_IsSubclass</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#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">derived</span></span>, <a class="reference internal" href="structures.html#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">cls</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyObject_IsSubclass" title="Link to this definition">¶</a><br /></dt>
  392. <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>Return <code class="docutils literal notranslate"><span class="pre">1</span></code> if the class <em>derived</em> is identical to or derived from the class
  393. <em>cls</em>, otherwise return <code class="docutils literal notranslate"><span class="pre">0</span></code>. In case of an error, return <code class="docutils literal notranslate"><span class="pre">-1</span></code>.</p>
  394. <p>If <em>cls</em> is a tuple, the check will be done against every entry in <em>cls</em>.
  395. The result will be <code class="docutils literal notranslate"><span class="pre">1</span></code> when at least one of the checks returns <code class="docutils literal notranslate"><span class="pre">1</span></code>,
  396. otherwise it will be <code class="docutils literal notranslate"><span class="pre">0</span></code>.</p>
  397. <p>If <em>cls</em> has a <a class="reference internal" href="../reference/datamodel.html#class.__subclasscheck__" title="class.__subclasscheck__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__subclasscheck__()</span></code></a> method, it will be called to
  398. determine the subclass status as described in <span class="target" id="index-4"></span><a class="pep reference external" href="https://peps.python.org/pep-3119/"><strong>PEP 3119</strong></a>. Otherwise,
  399. <em>derived</em> is a subclass of <em>cls</em> if it is a direct or indirect subclass,
  400. i.e. contained in <code class="docutils literal notranslate"><span class="pre">cls.__mro__</span></code>.</p>
  401. <p>Normally only class objects, i.e. instances of <a class="reference internal" href="../library/functions.html#type" title="type"><code class="xref py py-class docutils literal notranslate"><span class="pre">type</span></code></a> or a derived
  402. class, are considered classes. However, objects can override this by having
  403. a <a class="reference internal" href="../library/stdtypes.html#class.__bases__" title="class.__bases__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__bases__</span></code></a> attribute (which must be a tuple of base classes).</p>
  404. </dd></dl>
  405. <dl class="c function">
  406. <dt class="sig sig-object c" id="c.PyObject_IsInstance">
  407. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyObject_IsInstance</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#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">inst</span></span>, <a class="reference internal" href="structures.html#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">cls</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyObject_IsInstance" title="Link to this definition">¶</a><br /></dt>
  408. <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>Return <code class="docutils literal notranslate"><span class="pre">1</span></code> if <em>inst</em> is an instance of the class <em>cls</em> or a subclass of
  409. <em>cls</em>, or <code class="docutils literal notranslate"><span class="pre">0</span></code> if not. On error, returns <code class="docutils literal notranslate"><span class="pre">-1</span></code> and sets an exception.</p>
  410. <p>If <em>cls</em> is a tuple, the check will be done against every entry in <em>cls</em>.
  411. The result will be <code class="docutils literal notranslate"><span class="pre">1</span></code> when at least one of the checks returns <code class="docutils literal notranslate"><span class="pre">1</span></code>,
  412. otherwise it will be <code class="docutils literal notranslate"><span class="pre">0</span></code>.</p>
  413. <p>If <em>cls</em> has a <a class="reference internal" href="../reference/datamodel.html#class.__instancecheck__" title="class.__instancecheck__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__instancecheck__()</span></code></a> method, it will be called to
  414. determine the subclass status as described in <span class="target" id="index-5"></span><a class="pep reference external" href="https://peps.python.org/pep-3119/"><strong>PEP 3119</strong></a>. Otherwise, <em>inst</em>
  415. is an instance of <em>cls</em> if its class is a subclass of <em>cls</em>.</p>
  416. <p>An instance <em>inst</em> can override what is considered its class by having a
  417. <a class="reference internal" href="../library/stdtypes.html#instance.__class__" title="instance.__class__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__class__</span></code></a> attribute.</p>
  418. <p>An object <em>cls</em> can override if it is considered a class, and what its base
  419. classes are, by having a <a class="reference internal" href="../library/stdtypes.html#class.__bases__" title="class.__bases__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__bases__</span></code></a> attribute (which must be a tuple
  420. of base classes).</p>
  421. </dd></dl>
  422. <dl class="c function">
  423. <dt class="sig sig-object c" id="c.PyObject_Hash">
  424. <a class="reference internal" href="hash.html#c.Py_hash_t" title="Py_hash_t"><span class="n"><span class="pre">Py_hash_t</span></span></a><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyObject_Hash</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#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.PyObject_Hash" title="Link to this definition">¶</a><br /></dt>
  425. <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 id="index-6">Compute and return the hash value of an object <em>o</em>. On failure, return <code class="docutils literal notranslate"><span class="pre">-1</span></code>.
  426. This is the equivalent of the Python expression <code class="docutils literal notranslate"><span class="pre">hash(o)</span></code>.</p>
  427. <div class="versionchanged">
  428. <p><span class="versionmodified changed">Changed in version 3.2: </span>The return type is now Py_hash_t. This is a signed integer the same size
  429. as <a class="reference internal" href="intro.html#c.Py_ssize_t" title="Py_ssize_t"><code class="xref c c-type docutils literal notranslate"><span class="pre">Py_ssize_t</span></code></a>.</p>
  430. </div>
  431. </dd></dl>
  432. <dl class="c function">
  433. <dt class="sig sig-object c" id="c.PyObject_HashNotImplemented">
  434. <a class="reference internal" href="hash.html#c.Py_hash_t" title="Py_hash_t"><span class="n"><span class="pre">Py_hash_t</span></span></a><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyObject_HashNotImplemented</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#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.PyObject_HashNotImplemented" title="Link to this definition">¶</a><br /></dt>
  435. <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 a <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> indicating that <code class="docutils literal notranslate"><span class="pre">type(o)</span></code> is not <a class="reference internal" href="../glossary.html#term-hashable"><span class="xref std std-term">hashable</span></a> and return <code class="docutils literal notranslate"><span class="pre">-1</span></code>.
  436. This function receives special treatment when stored in a <code class="docutils literal notranslate"><span class="pre">tp_hash</span></code> slot,
  437. allowing a type to explicitly indicate to the interpreter that it is not
  438. hashable.</p>
  439. </dd></dl>
  440. <dl class="c function">
  441. <dt class="sig sig-object c" id="c.PyObject_IsTrue">
  442. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyObject_IsTrue</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#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.PyObject_IsTrue" title="Link to this definition">¶</a><br /></dt>
  443. <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>Returns <code class="docutils literal notranslate"><span class="pre">1</span></code> if the object <em>o</em> is considered to be true, and <code class="docutils literal notranslate"><span class="pre">0</span></code> otherwise.
  444. This is equivalent to the Python expression <code class="docutils literal notranslate"><span class="pre">not</span> <span class="pre">not</span> <span class="pre">o</span></code>. On failure, return
  445. <code class="docutils literal notranslate"><span class="pre">-1</span></code>.</p>
  446. </dd></dl>
  447. <dl class="c function">
  448. <dt class="sig sig-object c" id="c.PyObject_Not">
  449. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyObject_Not</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#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.PyObject_Not" title="Link to this definition">¶</a><br /></dt>
  450. <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>Returns <code class="docutils literal notranslate"><span class="pre">0</span></code> if the object <em>o</em> is considered to be true, and <code class="docutils literal notranslate"><span class="pre">1</span></code> otherwise.
  451. This is equivalent to the Python expression <code class="docutils literal notranslate"><span class="pre">not</span> <span class="pre">o</span></code>. On failure, return
  452. <code class="docutils literal notranslate"><span class="pre">-1</span></code>.</p>
  453. </dd></dl>
  454. <dl class="c function">
  455. <dt class="sig sig-object c" id="c.PyObject_Type">
  456. <a class="reference internal" href="structures.html#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">PyObject_Type</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#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.PyObject_Type" title="Link to this definition">¶</a><br /></dt>
  457. <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 id="index-7">When <em>o</em> is non-<code class="docutils literal notranslate"><span class="pre">NULL</span></code>, returns a type object corresponding to the object type
  458. of object <em>o</em>. On failure, raises <a class="reference internal" href="../library/exceptions.html#SystemError" title="SystemError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SystemError</span></code></a> and returns <code class="docutils literal notranslate"><span class="pre">NULL</span></code>. This
  459. is equivalent to the Python expression <code class="docutils literal notranslate"><span class="pre">type(o)</span></code>.
  460. This function creates a new <a class="reference internal" href="../glossary.html#term-strong-reference"><span class="xref std std-term">strong reference</span></a> to the return value.
  461. There’s really no reason to use this
  462. function instead of the <a class="reference internal" href="structures.html#c.Py_TYPE" title="Py_TYPE"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_TYPE()</span></code></a> function, which returns a
  463. pointer of type <span class="c-expr sig sig-inline c"><a class="reference internal" href="type.html#c.PyTypeObject" title="PyTypeObject"><span class="n">PyTypeObject</span></a><span class="p">*</span></span>, except when a new
  464. <a class="reference internal" href="../glossary.html#term-strong-reference"><span class="xref std std-term">strong reference</span></a> is needed.</p>
  465. </dd></dl>
  466. <dl class="c function">
  467. <dt class="sig sig-object c" id="c.PyObject_TypeCheck">
  468. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyObject_TypeCheck</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#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.PyObject_TypeCheck" title="Link to this definition">¶</a><br /></dt>
  469. <dd><p>Return non-zero if the object <em>o</em> is of type <em>type</em> or a subtype of <em>type</em>, and
  470. <code class="docutils literal notranslate"><span class="pre">0</span></code> otherwise. Both parameters must be non-<code class="docutils literal notranslate"><span class="pre">NULL</span></code>.</p>
  471. </dd></dl>
  472. <dl class="c function">
  473. <dt class="sig sig-object c" id="c.PyObject_Size">
  474. <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">PyObject_Size</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#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.PyObject_Size" title="Link to this definition">¶</a><br /></dt>
  475. <dt class="sig sig-object c" id="c.PyObject_Length">
  476. <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">PyObject_Length</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#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.PyObject_Length" title="Link to this definition">¶</a><br /></dt>
  477. <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 id="index-8">Return the length of object <em>o</em>. If the object <em>o</em> provides either the sequence
  478. and mapping protocols, the sequence length is returned. On error, <code class="docutils literal notranslate"><span class="pre">-1</span></code> is
  479. returned. This is the equivalent to the Python expression <code class="docutils literal notranslate"><span class="pre">len(o)</span></code>.</p>
  480. </dd></dl>
  481. <dl class="c function">
  482. <dt class="sig sig-object c" id="c.PyObject_LengthHint">
  483. <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">PyObject_LengthHint</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#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="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">defaultvalue</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyObject_LengthHint" title="Link to this definition">¶</a><br /></dt>
  484. <dd><p>Return an estimated length for the object <em>o</em>. First try to return its
  485. actual length, then an estimate using <a class="reference internal" href="../reference/datamodel.html#object.__length_hint__" title="object.__length_hint__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__length_hint__()</span></code></a>, and
  486. finally return the default value. On error return <code class="docutils literal notranslate"><span class="pre">-1</span></code>. This is the
  487. equivalent to the Python expression <code class="docutils literal notranslate"><span class="pre">operator.length_hint(o,</span> <span class="pre">defaultvalue)</span></code>.</p>
  488. <div class="versionadded">
  489. <p><span class="versionmodified added">New in version 3.4.</span></p>
  490. </div>
  491. </dd></dl>
  492. <dl class="c function">
  493. <dt class="sig sig-object c" id="c.PyObject_GetItem">
  494. <a class="reference internal" href="structures.html#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">PyObject_GetItem</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#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="structures.html#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">key</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyObject_GetItem" title="Link to this definition">¶</a><br /></dt>
  495. <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>Return element of <em>o</em> corresponding to the object <em>key</em> or <code class="docutils literal notranslate"><span class="pre">NULL</span></code> on failure.
  496. This is the equivalent of the Python expression <code class="docutils literal notranslate"><span class="pre">o[key]</span></code>.</p>
  497. </dd></dl>
  498. <dl class="c function">
  499. <dt class="sig sig-object c" id="c.PyObject_SetItem">
  500. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyObject_SetItem</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#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="structures.html#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">key</span></span>, <a class="reference internal" href="structures.html#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">v</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyObject_SetItem" title="Link to this definition">¶</a><br /></dt>
  501. <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>Map the object <em>key</em> to the value <em>v</em>. Raise an exception and
  502. return <code class="docutils literal notranslate"><span class="pre">-1</span></code> on failure; return <code class="docutils literal notranslate"><span class="pre">0</span></code> on success. This is the
  503. equivalent of the Python statement <code class="docutils literal notranslate"><span class="pre">o[key]</span> <span class="pre">=</span> <span class="pre">v</span></code>. This function <em>does
  504. not</em> steal a reference to <em>v</em>.</p>
  505. </dd></dl>
  506. <dl class="c function">
  507. <dt class="sig sig-object c" id="c.PyObject_DelItem">
  508. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyObject_DelItem</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#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="structures.html#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">key</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyObject_DelItem" title="Link to this definition">¶</a><br /></dt>
  509. <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>Remove the mapping for the object <em>key</em> from the object <em>o</em>. Return <code class="docutils literal notranslate"><span class="pre">-1</span></code>
  510. on failure. This is equivalent to the Python statement <code class="docutils literal notranslate"><span class="pre">del</span> <span class="pre">o[key]</span></code>.</p>
  511. </dd></dl>
  512. <dl class="c function">
  513. <dt class="sig sig-object c" id="c.PyObject_Dir">
  514. <a class="reference internal" href="structures.html#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">PyObject_Dir</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#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.PyObject_Dir" title="Link to this definition">¶</a><br /></dt>
  515. <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>This is equivalent to the Python expression <code class="docutils literal notranslate"><span class="pre">dir(o)</span></code>, returning a (possibly
  516. empty) list of strings appropriate for the object argument, or <code class="docutils literal notranslate"><span class="pre">NULL</span></code> if there
  517. was an error. If the argument is <code class="docutils literal notranslate"><span class="pre">NULL</span></code>, this is like the Python <code class="docutils literal notranslate"><span class="pre">dir()</span></code>,
  518. returning the names of the current locals; in this case, if no execution frame
  519. is active then <code class="docutils literal notranslate"><span class="pre">NULL</span></code> is returned but <a class="reference internal" href="exceptions.html#c.PyErr_Occurred" title="PyErr_Occurred"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyErr_Occurred()</span></code></a> will return false.</p>
  520. </dd></dl>
  521. <dl class="c function">
  522. <dt class="sig sig-object c" id="c.PyObject_GetIter">
  523. <a class="reference internal" href="structures.html#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">PyObject_GetIter</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#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.PyObject_GetIter" title="Link to this definition">¶</a><br /></dt>
  524. <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>This is equivalent to the Python expression <code class="docutils literal notranslate"><span class="pre">iter(o)</span></code>. It returns a new
  525. iterator for the object argument, or the object itself if the object is already
  526. an iterator. Raises <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> and returns <code class="docutils literal notranslate"><span class="pre">NULL</span></code> if the object cannot be
  527. iterated.</p>
  528. </dd></dl>
  529. <dl class="c function">
  530. <dt class="sig sig-object c" id="c.PyObject_GetAIter">
  531. <a class="reference internal" href="structures.html#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">PyObject_GetAIter</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#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.PyObject_GetAIter" title="Link to this definition">¶</a><br /></dt>
  532. <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.10.</em><p>This is the equivalent to the Python expression <code class="docutils literal notranslate"><span class="pre">aiter(o)</span></code>. Takes an
  533. <code class="xref py py-class docutils literal notranslate"><span class="pre">AsyncIterable</span></code> object and returns an <code class="xref py py-class docutils literal notranslate"><span class="pre">AsyncIterator</span></code> for it.
  534. This is typically a new iterator but if the argument is an
  535. <code class="xref py py-class docutils literal notranslate"><span class="pre">AsyncIterator</span></code>, this returns itself. Raises <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> and
  536. returns <code class="docutils literal notranslate"><span class="pre">NULL</span></code> if the object cannot be iterated.</p>
  537. <div class="versionadded">
  538. <p><span class="versionmodified added">New in version 3.10.</span></p>
  539. </div>
  540. </dd></dl>
  541. <dl class="c function">
  542. <dt class="sig sig-object c" id="c.PyObject_GetTypeData">
  543. <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">PyObject_GetTypeData</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#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">cls</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyObject_GetTypeData" title="Link to this definition">¶</a><br /></dt>
  544. <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.12.</em><p>Get a pointer to subclass-specific data reserved for <em>cls</em>.</p>
  545. <p>The object <em>o</em> must be an instance of <em>cls</em>, and <em>cls</em> must have been
  546. created using negative <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">PyType_Spec.basicsize</span></code></a>.
  547. Python does not check this.</p>
  548. <p>On error, set an exception and return <code class="docutils literal notranslate"><span class="pre">NULL</span></code>.</p>
  549. <div class="versionadded">
  550. <p><span class="versionmodified added">New in version 3.12.</span></p>
  551. </div>
  552. </dd></dl>
  553. <dl class="c function">
  554. <dt class="sig sig-object c" id="c.PyType_GetTypeDataSize">
  555. <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">PyType_GetTypeDataSize</span></span></span><span class="sig-paren">(</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.PyType_GetTypeDataSize" title="Link to this definition">¶</a><br /></dt>
  556. <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.12.</em><p>Return the size of the instance memory space reserved for <em>cls</em>, i.e. the size of the
  557. memory <a class="reference internal" href="#c.PyObject_GetTypeData" title="PyObject_GetTypeData"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_GetTypeData()</span></code></a> returns.</p>
  558. <p>This may be larger than requested using <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">-PyType_Spec.basicsize</span></code></a>;
  559. it is safe to use this larger size (e.g. with <code class="xref c c-func docutils literal notranslate"><span class="pre">memset()</span></code>).</p>
  560. <p>The type <em>cls</em> <strong>must</strong> have been created using
  561. negative <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">PyType_Spec.basicsize</span></code></a>.
  562. Python does not check this.</p>
  563. <p>On error, set an exception and return a negative value.</p>
  564. <div class="versionadded">
  565. <p><span class="versionmodified added">New in version 3.12.</span></p>
  566. </div>
  567. </dd></dl>
  568. <dl class="c function">
  569. <dt class="sig sig-object c" id="c.PyObject_GetItemData">
  570. <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">PyObject_GetItemData</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#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.PyObject_GetItemData" title="Link to this definition">¶</a><br /></dt>
  571. <dd><p>Get a pointer to per-item data for a class with
  572. <a class="reference internal" href="typeobj.html#c.Py_TPFLAGS_ITEMS_AT_END" title="Py_TPFLAGS_ITEMS_AT_END"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_TPFLAGS_ITEMS_AT_END</span></code></a>.</p>
  573. <p>On error, set an exception and return <code class="docutils literal notranslate"><span class="pre">NULL</span></code>.
  574. <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> is raised if <em>o</em> does not have
  575. <a class="reference internal" href="typeobj.html#c.Py_TPFLAGS_ITEMS_AT_END" title="Py_TPFLAGS_ITEMS_AT_END"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_TPFLAGS_ITEMS_AT_END</span></code></a> set.</p>
  576. <div class="versionadded">
  577. <p><span class="versionmodified added">New in version 3.12.</span></p>
  578. </div>
  579. </dd></dl>
  580. </section>
  581. <div class="clearer"></div>
  582. </div>
  583. </div>
  584. </div>
  585. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  586. <div class="sphinxsidebarwrapper">
  587. <div>
  588. <h4>Previous topic</h4>
  589. <p class="topless"><a href="abstract.html"
  590. title="previous chapter">Abstract Objects Layer</a></p>
  591. </div>
  592. <div>
  593. <h4>Next topic</h4>
  594. <p class="topless"><a href="call.html"
  595. title="next chapter">Call Protocol</a></p>
  596. </div>
  597. <div role="note" aria-label="source link">
  598. <h3>This Page</h3>
  599. <ul class="this-page-menu">
  600. <li><a href="../bugs.html">Report a Bug</a></li>
  601. <li>
  602. <a href="https://github.com/python/cpython/blob/main/Doc/c-api/object.rst"
  603. rel="nofollow">Show Source
  604. </a>
  605. </li>
  606. </ul>
  607. </div>
  608. </div>
  609. <div id="sidebarbutton" title="Collapse sidebar">
  610. <span>«</span>
  611. </div>
  612. </div>
  613. <div class="clearer"></div>
  614. </div>
  615. <div class="related" role="navigation" aria-label="related navigation">
  616. <h3>Navigation</h3>
  617. <ul>
  618. <li class="right" style="margin-right: 10px">
  619. <a href="../genindex.html" title="General Index"
  620. >index</a></li>
  621. <li class="right" >
  622. <a href="../py-modindex.html" title="Python Module Index"
  623. >modules</a> |</li>
  624. <li class="right" >
  625. <a href="call.html" title="Call Protocol"
  626. >next</a> |</li>
  627. <li class="right" >
  628. <a href="abstract.html" title="Abstract Objects Layer"
  629. >previous</a> |</li>
  630. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  631. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  632. <li class="switchers">
  633. <div class="language_switcher_placeholder"></div>
  634. <div class="version_switcher_placeholder"></div>
  635. </li>
  636. <li>
  637. </li>
  638. <li id="cpython-language-and-version">
  639. <a href="../index.html">3.12.3 Documentation</a> &#187;
  640. </li>
  641. <li class="nav-item nav-item-1"><a href="index.html" >Python/C API Reference Manual</a> &#187;</li>
  642. <li class="nav-item nav-item-2"><a href="abstract.html" >Abstract Objects Layer</a> &#187;</li>
  643. <li class="nav-item nav-item-this"><a href="">Object Protocol</a></li>
  644. <li class="right">
  645. <div class="inline-search" role="search">
  646. <form class="inline-search" action="../search.html" method="get">
  647. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  648. <input type="submit" value="Go" />
  649. </form>
  650. </div>
  651. |
  652. </li>
  653. <li class="right">
  654. <label class="theme-selector-label">
  655. Theme
  656. <select class="theme-selector" oninput="activateTheme(this.value)">
  657. <option value="auto" selected>Auto</option>
  658. <option value="light">Light</option>
  659. <option value="dark">Dark</option>
  660. </select>
  661. </label> |</li>
  662. </ul>
  663. </div>
  664. <div class="footer">
  665. &copy;
  666. <a href="../copyright.html">
  667. Copyright
  668. </a>
  669. 2001-2024, Python Software Foundation.
  670. <br />
  671. This page is licensed under the Python Software Foundation License Version 2.
  672. <br />
  673. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  674. <br />
  675. See <a href="/license.html">History and License</a> for more information.<br />
  676. <br />
  677. The Python Software Foundation is a non-profit corporation.
  678. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  679. <br />
  680. <br />
  681. Last updated on Apr 09, 2024 (13:47 UTC).
  682. <a href="/bugs.html">Found a bug</a>?
  683. <br />
  684. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  685. </div>
  686. </body>
  687. </html>
上海开阖软件有限公司 沪ICP备12045867号-1