1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5
<html xmlns="http://www.w3.org/1999/xhtml">
7
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
9
<title>Type Objects — Python 3.5.1 documentation</title>
11
<link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
12
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
14
<script type="text/javascript">
15
var DOCUMENTATION_OPTIONS = {
18
COLLAPSE_INDEX: false,
23
<script type="text/javascript" src="../_static/jquery.js"></script>
24
<script type="text/javascript" src="../_static/underscore.js"></script>
25
<script type="text/javascript" src="../_static/doctools.js"></script>
26
<script type="text/javascript" src="../_static/sidebar.js"></script>
27
<link rel="search" type="application/opensearchdescription+xml"
28
title="Search within Python 3.5.1 documentation"
29
href="../_static/opensearch.xml"/>
30
<link rel="author" title="About these documents" href="../about.html" />
31
<link rel="copyright" title="Copyright" href="../copyright.html" />
32
<link rel="top" title="Python 3.5.1 documentation" href="../contents.html" />
33
<link rel="up" title="Object Implementation Support" href="objimpl.html" />
34
<link rel="next" title="Supporting Cyclic Garbage Collection" href="gcsupport.html" />
35
<link rel="prev" title="Common Object Structures" href="structures.html" />
36
<link rel="shortcut icon" type="image/png" href="../_static/py.png" />
37
<script type="text/javascript" src="../_static/copybutton.js"></script>
38
<script type="text/javascript" src="../_static/version_switch.js"></script>
43
<body role="document">
44
<div class="related" role="navigation" aria-label="related navigation">
47
<li class="right" style="margin-right: 10px">
48
<a href="../genindex.html" title="General Index"
49
accesskey="I">index</a></li>
51
<a href="../py-modindex.html" title="Python Module Index"
54
<a href="gcsupport.html" title="Supporting Cyclic Garbage Collection"
55
accesskey="N">next</a> |</li>
57
<a href="structures.html" title="Common Object Structures"
58
accesskey="P">previous</a> |</li>
59
<li><img src="../_static/py.png" alt=""
60
style="vertical-align: middle; margin-top: -1px"/></li>
61
<li><a href="https://www.python.org/">Python</a> »</li>
63
<span class="version_switcher_placeholder">3.5.1</span>
64
<a href="../index.html">Documentation </a> »
67
<li class="nav-item nav-item-1"><a href="index.html" >Python/C API Reference Manual</a> »</li>
68
<li class="nav-item nav-item-2"><a href="objimpl.html" accesskey="U">Object Implementation Support</a> »</li>
72
<div class="document">
73
<div class="documentwrapper">
74
<div class="bodywrapper">
75
<div class="body" role="main">
77
<div class="section" id="type-objects">
78
<span id="type-structs"></span><h1>Type Objects<a class="headerlink" href="#type-objects" title="Permalink to this headline">¶</a></h1>
79
<p>Perhaps one of the most important structures of the Python object system is the
80
structure that defines a new type: the <a class="reference internal" href="type.html#c.PyTypeObject" title="PyTypeObject"><code class="xref c c-type docutils literal"><span class="pre">PyTypeObject</span></code></a> structure. Type
81
objects can be handled using any of the <code class="xref c c-func docutils literal"><span class="pre">PyObject_*()</span></code> or
82
<code class="xref c c-func docutils literal"><span class="pre">PyType_*()</span></code> functions, but do not offer much that’s interesting to most
83
Python applications. These objects are fundamental to how objects behave, so
84
they are very important to the interpreter itself and to any extension module
85
that implements new types.</p>
86
<p>Type objects are fairly large compared to most of the standard types. The reason
87
for the size is that each type object stores a large number of values, mostly C
88
function pointers, each of which implements a small part of the type’s
89
functionality. The fields of the type object are examined in detail in this
90
section. The fields will be described in the order in which they occur in the
92
<p>Typedefs: unaryfunc, binaryfunc, ternaryfunc, inquiry, intargfunc,
93
intintargfunc, intobjargproc, intintobjargproc, objobjargproc, destructor,
94
freefunc, printfunc, getattrfunc, getattrofunc, setattrfunc, setattrofunc,
95
reprfunc, hashfunc</p>
96
<p>The structure definition for <a class="reference internal" href="type.html#c.PyTypeObject" title="PyTypeObject"><code class="xref c c-type docutils literal"><span class="pre">PyTypeObject</span></code></a> can be found in
97
<code class="file docutils literal"><span class="pre">Include/object.h</span></code>. For convenience of reference, this repeats the
98
definition found there:</p>
99
<div class="highlight-c"><div class="highlight"><pre><span class="k">typedef</span> <span class="k">struct</span> <span class="n">_typeobject</span> <span class="p">{</span>
100
<span class="n">PyObject_VAR_HEAD</span>
101
<span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">tp_name</span><span class="p">;</span> <span class="cm">/* For printing, in format "<module>.<name>" */</span>
102
<span class="n">Py_ssize_t</span> <span class="n">tp_basicsize</span><span class="p">,</span> <span class="n">tp_itemsize</span><span class="p">;</span> <span class="cm">/* For allocation */</span>
104
<span class="cm">/* Methods to implement standard operations */</span>
106
<span class="n">destructor</span> <span class="n">tp_dealloc</span><span class="p">;</span>
107
<span class="n">printfunc</span> <span class="n">tp_print</span><span class="p">;</span>
108
<span class="n">getattrfunc</span> <span class="n">tp_getattr</span><span class="p">;</span>
109
<span class="n">setattrfunc</span> <span class="n">tp_setattr</span><span class="p">;</span>
110
<span class="n">PyAsyncMethods</span> <span class="o">*</span><span class="n">tp_as_async</span><span class="p">;</span> <span class="cm">/* formerly known as tp_compare (Python 2)</span>
111
<span class="cm"> or tp_reserved (Python 3) */</span>
112
<span class="n">reprfunc</span> <span class="n">tp_repr</span><span class="p">;</span>
114
<span class="cm">/* Method suites for standard classes */</span>
116
<span class="n">PyNumberMethods</span> <span class="o">*</span><span class="n">tp_as_number</span><span class="p">;</span>
117
<span class="n">PySequenceMethods</span> <span class="o">*</span><span class="n">tp_as_sequence</span><span class="p">;</span>
118
<span class="n">PyMappingMethods</span> <span class="o">*</span><span class="n">tp_as_mapping</span><span class="p">;</span>
120
<span class="cm">/* More standard operations (here for binary compatibility) */</span>
122
<span class="n">hashfunc</span> <span class="n">tp_hash</span><span class="p">;</span>
123
<span class="n">ternaryfunc</span> <span class="n">tp_call</span><span class="p">;</span>
124
<span class="n">reprfunc</span> <span class="n">tp_str</span><span class="p">;</span>
125
<span class="n">getattrofunc</span> <span class="n">tp_getattro</span><span class="p">;</span>
126
<span class="n">setattrofunc</span> <span class="n">tp_setattro</span><span class="p">;</span>
128
<span class="cm">/* Functions to access object as input/output buffer */</span>
129
<span class="n">PyBufferProcs</span> <span class="o">*</span><span class="n">tp_as_buffer</span><span class="p">;</span>
131
<span class="cm">/* Flags to define presence of optional/expanded features */</span>
132
<span class="kt">unsigned</span> <span class="kt">long</span> <span class="n">tp_flags</span><span class="p">;</span>
134
<span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">tp_doc</span><span class="p">;</span> <span class="cm">/* Documentation string */</span>
136
<span class="cm">/* call function for all accessible objects */</span>
137
<span class="n">traverseproc</span> <span class="n">tp_traverse</span><span class="p">;</span>
139
<span class="cm">/* delete references to contained objects */</span>
140
<span class="n">inquiry</span> <span class="n">tp_clear</span><span class="p">;</span>
142
<span class="cm">/* rich comparisons */</span>
143
<span class="n">richcmpfunc</span> <span class="n">tp_richcompare</span><span class="p">;</span>
145
<span class="cm">/* weak reference enabler */</span>
146
<span class="n">Py_ssize_t</span> <span class="n">tp_weaklistoffset</span><span class="p">;</span>
148
<span class="cm">/* Iterators */</span>
149
<span class="n">getiterfunc</span> <span class="n">tp_iter</span><span class="p">;</span>
150
<span class="n">iternextfunc</span> <span class="n">tp_iternext</span><span class="p">;</span>
152
<span class="cm">/* Attribute descriptor and subclassing stuff */</span>
153
<span class="k">struct</span> <span class="n">PyMethodDef</span> <span class="o">*</span><span class="n">tp_methods</span><span class="p">;</span>
154
<span class="k">struct</span> <span class="n">PyMemberDef</span> <span class="o">*</span><span class="n">tp_members</span><span class="p">;</span>
155
<span class="k">struct</span> <span class="n">PyGetSetDef</span> <span class="o">*</span><span class="n">tp_getset</span><span class="p">;</span>
156
<span class="k">struct</span> <span class="n">_typeobject</span> <span class="o">*</span><span class="n">tp_base</span><span class="p">;</span>
157
<span class="n">PyObject</span> <span class="o">*</span><span class="n">tp_dict</span><span class="p">;</span>
158
<span class="n">descrgetfunc</span> <span class="n">tp_descr_get</span><span class="p">;</span>
159
<span class="n">descrsetfunc</span> <span class="n">tp_descr_set</span><span class="p">;</span>
160
<span class="n">Py_ssize_t</span> <span class="n">tp_dictoffset</span><span class="p">;</span>
161
<span class="n">initproc</span> <span class="n">tp_init</span><span class="p">;</span>
162
<span class="n">allocfunc</span> <span class="n">tp_alloc</span><span class="p">;</span>
163
<span class="n">newfunc</span> <span class="n">tp_new</span><span class="p">;</span>
164
<span class="n">freefunc</span> <span class="n">tp_free</span><span class="p">;</span> <span class="cm">/* Low-level free-memory routine */</span>
165
<span class="n">inquiry</span> <span class="n">tp_is_gc</span><span class="p">;</span> <span class="cm">/* For PyObject_IS_GC */</span>
166
<span class="n">PyObject</span> <span class="o">*</span><span class="n">tp_bases</span><span class="p">;</span>
167
<span class="n">PyObject</span> <span class="o">*</span><span class="n">tp_mro</span><span class="p">;</span> <span class="cm">/* method resolution order */</span>
168
<span class="n">PyObject</span> <span class="o">*</span><span class="n">tp_cache</span><span class="p">;</span>
169
<span class="n">PyObject</span> <span class="o">*</span><span class="n">tp_subclasses</span><span class="p">;</span>
170
<span class="n">PyObject</span> <span class="o">*</span><span class="n">tp_weaklist</span><span class="p">;</span>
171
<span class="n">destructor</span> <span class="n">tp_del</span><span class="p">;</span>
173
<span class="cm">/* Type attribute cache version tag. Added in version 2.6 */</span>
174
<span class="kt">unsigned</span> <span class="kt">int</span> <span class="n">tp_version_tag</span><span class="p">;</span>
176
<span class="n">destructor</span> <span class="n">tp_finalize</span><span class="p">;</span>
178
<span class="p">}</span> <span class="n">PyTypeObject</span><span class="p">;</span>
181
<p>The type object structure extends the <a class="reference internal" href="structures.html#c.PyVarObject" title="PyVarObject"><code class="xref c c-type docutils literal"><span class="pre">PyVarObject</span></code></a> structure. The
182
<code class="xref py py-attr docutils literal"><span class="pre">ob_size</span></code> field is used for dynamic types (created by <code class="xref py py-func docutils literal"><span class="pre">type_new()</span></code>,
183
usually called from a class statement). Note that <a class="reference internal" href="type.html#c.PyType_Type" title="PyType_Type"><code class="xref c c-data docutils literal"><span class="pre">PyType_Type</span></code></a> (the
184
metatype) initializes <a class="reference internal" href="#c.PyTypeObject.tp_itemsize" title="PyTypeObject.tp_itemsize"><code class="xref c c-member docutils literal"><span class="pre">tp_itemsize</span></code></a>, which means that its instances (i.e.
185
type objects) <em>must</em> have the <code class="xref py py-attr docutils literal"><span class="pre">ob_size</span></code> field.</p>
187
<dt id="c.PyObject._ob_next">
188
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyObject._ob_next</code><a class="headerlink" href="#c.PyObject._ob_next" title="Permalink to this definition">¶</a></dt>
189
<dt id="c.PyObject._ob_prev">
190
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyObject._ob_prev</code><a class="headerlink" href="#c.PyObject._ob_prev" title="Permalink to this definition">¶</a></dt>
191
<dd><p>These fields are only present when the macro <code class="docutils literal"><span class="pre">Py_TRACE_REFS</span></code> is defined.
192
Their initialization to <em>NULL</em> is taken care of by the <code class="docutils literal"><span class="pre">PyObject_HEAD_INIT</span></code>
193
macro. For statically allocated objects, these fields always remain <em>NULL</em>.
194
For dynamically allocated objects, these two fields are used to link the object
195
into a doubly-linked list of <em>all</em> live objects on the heap. This could be used
196
for various debugging purposes; currently the only use is to print the objects
197
that are still alive at the end of a run when the environment variable
198
<span class="target" id="index-0"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONDUMPREFS"><code class="xref std std-envvar docutils literal"><span class="pre">PYTHONDUMPREFS</span></code></a> is set.</p>
199
<p>These fields are not inherited by subtypes.</p>
203
<dt id="c.PyObject.ob_refcnt">
204
Py_ssize_t <code class="descname">PyObject.ob_refcnt</code><a class="headerlink" href="#c.PyObject.ob_refcnt" title="Permalink to this definition">¶</a></dt>
205
<dd><p>This is the type object’s reference count, initialized to <code class="docutils literal"><span class="pre">1</span></code> by the
206
<code class="docutils literal"><span class="pre">PyObject_HEAD_INIT</span></code> macro. Note that for statically allocated type objects,
207
the type’s instances (objects whose <code class="xref py py-attr docutils literal"><span class="pre">ob_type</span></code> points back to the type) do
208
<em>not</em> count as references. But for dynamically allocated type objects, the
209
instances <em>do</em> count as references.</p>
210
<p>This field is not inherited by subtypes.</p>
214
<dt id="c.PyObject.ob_type">
215
<a class="reference internal" href="type.html#c.PyTypeObject" title="PyTypeObject">PyTypeObject</a>* <code class="descname">PyObject.ob_type</code><a class="headerlink" href="#c.PyObject.ob_type" title="Permalink to this definition">¶</a></dt>
216
<dd><p>This is the type’s type, in other words its metatype. It is initialized by the
217
argument to the <code class="docutils literal"><span class="pre">PyObject_HEAD_INIT</span></code> macro, and its value should normally be
218
<code class="docutils literal"><span class="pre">&PyType_Type</span></code>. However, for dynamically loadable extension modules that must
219
be usable on Windows (at least), the compiler complains that this is not a valid
220
initializer. Therefore, the convention is to pass <em>NULL</em> to the
221
<code class="docutils literal"><span class="pre">PyObject_HEAD_INIT</span></code> macro and to initialize this field explicitly at the
222
start of the module’s initialization function, before doing anything else. This
223
is typically done like this:</p>
224
<div class="highlight-c"><div class="highlight"><pre><span class="n">Foo_Type</span><span class="p">.</span><span class="n">ob_type</span> <span class="o">=</span> <span class="o">&</span><span class="n">PyType_Type</span><span class="p">;</span>
227
<p>This should be done before any instances of the type are created.
228
<a class="reference internal" href="type.html#c.PyType_Ready" title="PyType_Ready"><code class="xref c c-func docutils literal"><span class="pre">PyType_Ready()</span></code></a> checks if <code class="xref py py-attr docutils literal"><span class="pre">ob_type</span></code> is <em>NULL</em>, and if so,
229
initializes it to the <code class="xref py py-attr docutils literal"><span class="pre">ob_type</span></code> field of the base class.
230
<a class="reference internal" href="type.html#c.PyType_Ready" title="PyType_Ready"><code class="xref c c-func docutils literal"><span class="pre">PyType_Ready()</span></code></a> will not change this field if it is non-zero.</p>
231
<p>This field is inherited by subtypes.</p>
235
<dt id="c.PyVarObject.ob_size">
236
Py_ssize_t <code class="descname">PyVarObject.ob_size</code><a class="headerlink" href="#c.PyVarObject.ob_size" title="Permalink to this definition">¶</a></dt>
237
<dd><p>For statically allocated type objects, this should be initialized to zero. For
238
dynamically allocated type objects, this field has a special internal meaning.</p>
239
<p>This field is not inherited by subtypes.</p>
243
<dt id="c.PyTypeObject.tp_name">
244
const char* <code class="descname">PyTypeObject.tp_name</code><a class="headerlink" href="#c.PyTypeObject.tp_name" title="Permalink to this definition">¶</a></dt>
245
<dd><p>Pointer to a NUL-terminated string containing the name of the type. For types
246
that are accessible as module globals, the string should be the full module
247
name, followed by a dot, followed by the type name; for built-in types, it
248
should be just the type name. If the module is a submodule of a package, the
249
full package name is part of the full module name. For example, a type named
250
<code class="xref py py-class docutils literal"><span class="pre">T</span></code> defined in module <code class="xref py py-mod docutils literal"><span class="pre">M</span></code> in subpackage <code class="xref py py-mod docutils literal"><span class="pre">Q</span></code> in package <code class="xref py py-mod docutils literal"><span class="pre">P</span></code>
251
should have the <a class="reference internal" href="#c.PyTypeObject.tp_name" title="PyTypeObject.tp_name"><code class="xref c c-member docutils literal"><span class="pre">tp_name</span></code></a> initializer <code class="docutils literal"><span class="pre">"P.Q.M.T"</span></code>.</p>
252
<p>For dynamically allocated type objects, this should just be the type name, and
253
the module name explicitly stored in the type dict as the value for key
254
<code class="docutils literal"><span class="pre">'__module__'</span></code>.</p>
255
<p>For statically allocated type objects, the tp_name field should contain a dot.
256
Everything before the last dot is made accessible as the <code class="xref py py-attr docutils literal"><span class="pre">__module__</span></code>
257
attribute, and everything after the last dot is made accessible as the
258
<a class="reference internal" href="../reference/import.html#__name__" title="__name__"><code class="xref py py-attr docutils literal"><span class="pre">__name__</span></code></a> attribute.</p>
259
<p>If no dot is present, the entire <a class="reference internal" href="#c.PyTypeObject.tp_name" title="PyTypeObject.tp_name"><code class="xref c c-member docutils literal"><span class="pre">tp_name</span></code></a> field is made accessible as the
260
<a class="reference internal" href="../reference/import.html#__name__" title="__name__"><code class="xref py py-attr docutils literal"><span class="pre">__name__</span></code></a> attribute, and the <code class="xref py py-attr docutils literal"><span class="pre">__module__</span></code> attribute is undefined
261
(unless explicitly set in the dictionary, as explained above). This means your
262
type will be impossible to pickle.</p>
263
<p>This field is not inherited by subtypes.</p>
267
<dt id="c.PyTypeObject.tp_basicsize">
268
Py_ssize_t <code class="descname">PyTypeObject.tp_basicsize</code><a class="headerlink" href="#c.PyTypeObject.tp_basicsize" title="Permalink to this definition">¶</a></dt>
269
<dt id="c.PyTypeObject.tp_itemsize">
270
Py_ssize_t <code class="descname">PyTypeObject.tp_itemsize</code><a class="headerlink" href="#c.PyTypeObject.tp_itemsize" title="Permalink to this definition">¶</a></dt>
271
<dd><p>These fields allow calculating the size in bytes of instances of the type.</p>
272
<p>There are two kinds of types: types with fixed-length instances have a zero
273
<a class="reference internal" href="#c.PyTypeObject.tp_itemsize" title="PyTypeObject.tp_itemsize"><code class="xref c c-member docutils literal"><span class="pre">tp_itemsize</span></code></a> field, types with variable-length instances have a non-zero
274
<a class="reference internal" href="#c.PyTypeObject.tp_itemsize" title="PyTypeObject.tp_itemsize"><code class="xref c c-member docutils literal"><span class="pre">tp_itemsize</span></code></a> field. For a type with fixed-length instances, all
275
instances have the same size, given in <a class="reference internal" href="#c.PyTypeObject.tp_basicsize" title="PyTypeObject.tp_basicsize"><code class="xref c c-member docutils literal"><span class="pre">tp_basicsize</span></code></a>.</p>
276
<p>For a type with variable-length instances, the instances must have an
277
<code class="xref py py-attr docutils literal"><span class="pre">ob_size</span></code> field, and the instance size is <a class="reference internal" href="#c.PyTypeObject.tp_basicsize" title="PyTypeObject.tp_basicsize"><code class="xref c c-member docutils literal"><span class="pre">tp_basicsize</span></code></a> plus N
278
times <a class="reference internal" href="#c.PyTypeObject.tp_itemsize" title="PyTypeObject.tp_itemsize"><code class="xref c c-member docutils literal"><span class="pre">tp_itemsize</span></code></a>, where N is the “length” of the object. The value of
279
N is typically stored in the instance’s <code class="xref py py-attr docutils literal"><span class="pre">ob_size</span></code> field. There are
280
exceptions: for example, ints use a negative <code class="xref py py-attr docutils literal"><span class="pre">ob_size</span></code> to indicate a
281
negative number, and N is <code class="docutils literal"><span class="pre">abs(ob_size)</span></code> there. Also, the presence of an
282
<code class="xref py py-attr docutils literal"><span class="pre">ob_size</span></code> field in the instance layout doesn’t mean that the instance
283
structure is variable-length (for example, the structure for the list type has
284
fixed-length instances, yet those instances have a meaningful <code class="xref py py-attr docutils literal"><span class="pre">ob_size</span></code>
286
<p>The basic size includes the fields in the instance declared by the macro
287
<a class="reference internal" href="structures.html#c.PyObject_HEAD" title="PyObject_HEAD"><code class="xref c c-macro docutils literal"><span class="pre">PyObject_HEAD</span></code></a> or <a class="reference internal" href="structures.html#c.PyObject_VAR_HEAD" title="PyObject_VAR_HEAD"><code class="xref c c-macro docutils literal"><span class="pre">PyObject_VAR_HEAD</span></code></a> (whichever is used to
288
declare the instance struct) and this in turn includes the <code class="xref py py-attr docutils literal"><span class="pre">_ob_prev</span></code> and
289
<code class="xref py py-attr docutils literal"><span class="pre">_ob_next</span></code> fields if they are present. This means that the only correct
290
way to get an initializer for the <a class="reference internal" href="#c.PyTypeObject.tp_basicsize" title="PyTypeObject.tp_basicsize"><code class="xref c c-member docutils literal"><span class="pre">tp_basicsize</span></code></a> is to use the
291
<code class="docutils literal"><span class="pre">sizeof</span></code> operator on the struct used to declare the instance layout.
292
The basic size does not include the GC header size.</p>
293
<p>These fields are inherited separately by subtypes. If the base type has a
294
non-zero <a class="reference internal" href="#c.PyTypeObject.tp_itemsize" title="PyTypeObject.tp_itemsize"><code class="xref c c-member docutils literal"><span class="pre">tp_itemsize</span></code></a>, it is generally not safe to set
295
<a class="reference internal" href="#c.PyTypeObject.tp_itemsize" title="PyTypeObject.tp_itemsize"><code class="xref c c-member docutils literal"><span class="pre">tp_itemsize</span></code></a> to a different non-zero value in a subtype (though this
296
depends on the implementation of the base type).</p>
297
<p>A note about alignment: if the variable items require a particular alignment,
298
this should be taken care of by the value of <a class="reference internal" href="#c.PyTypeObject.tp_basicsize" title="PyTypeObject.tp_basicsize"><code class="xref c c-member docutils literal"><span class="pre">tp_basicsize</span></code></a>. Example:
299
suppose a type implements an array of <code class="docutils literal"><span class="pre">double</span></code>. <a class="reference internal" href="#c.PyTypeObject.tp_itemsize" title="PyTypeObject.tp_itemsize"><code class="xref c c-member docutils literal"><span class="pre">tp_itemsize</span></code></a> is
300
<code class="docutils literal"><span class="pre">sizeof(double)</span></code>. It is the programmer’s responsibility that
301
<a class="reference internal" href="#c.PyTypeObject.tp_basicsize" title="PyTypeObject.tp_basicsize"><code class="xref c c-member docutils literal"><span class="pre">tp_basicsize</span></code></a> is a multiple of <code class="docutils literal"><span class="pre">sizeof(double)</span></code> (assuming this is the
302
alignment requirement for <code class="docutils literal"><span class="pre">double</span></code>).</p>
306
<dt id="c.PyTypeObject.tp_dealloc">
307
destructor <code class="descname">PyTypeObject.tp_dealloc</code><a class="headerlink" href="#c.PyTypeObject.tp_dealloc" title="Permalink to this definition">¶</a></dt>
308
<dd><p>A pointer to the instance destructor function. This function must be defined
309
unless the type guarantees that its instances will never be deallocated (as is
310
the case for the singletons <code class="docutils literal"><span class="pre">None</span></code> and <code class="docutils literal"><span class="pre">Ellipsis</span></code>).</p>
311
<p>The destructor function is called by the <a class="reference internal" href="refcounting.html#c.Py_DECREF" title="Py_DECREF"><code class="xref c c-func docutils literal"><span class="pre">Py_DECREF()</span></code></a> and
312
<a class="reference internal" href="refcounting.html#c.Py_XDECREF" title="Py_XDECREF"><code class="xref c c-func docutils literal"><span class="pre">Py_XDECREF()</span></code></a> macros when the new reference count is zero. At this point,
313
the instance is still in existence, but there are no references to it. The
314
destructor function should free all references which the instance owns, free all
315
memory buffers owned by the instance (using the freeing function corresponding
316
to the allocation function used to allocate the buffer), and finally (as its
317
last action) call the type’s <a class="reference internal" href="#c.PyTypeObject.tp_free" title="PyTypeObject.tp_free"><code class="xref c c-member docutils literal"><span class="pre">tp_free</span></code></a> function. If the type is not
318
subtypable (doesn’t have the <a class="reference internal" href="#Py_TPFLAGS_BASETYPE" title="Py_TPFLAGS_BASETYPE"><code class="xref py py-const docutils literal"><span class="pre">Py_TPFLAGS_BASETYPE</span></code></a> flag bit set), it is
319
permissible to call the object deallocator directly instead of via
320
<a class="reference internal" href="#c.PyTypeObject.tp_free" title="PyTypeObject.tp_free"><code class="xref c c-member docutils literal"><span class="pre">tp_free</span></code></a>. The object deallocator should be the one used to allocate the
321
instance; this is normally <a class="reference internal" href="allocation.html#c.PyObject_Del" title="PyObject_Del"><code class="xref c c-func docutils literal"><span class="pre">PyObject_Del()</span></code></a> if the instance was allocated
322
using <a class="reference internal" href="allocation.html#c.PyObject_New" title="PyObject_New"><code class="xref c c-func docutils literal"><span class="pre">PyObject_New()</span></code></a> or <code class="xref c c-func docutils literal"><span class="pre">PyObject_VarNew()</span></code>, or
323
<a class="reference internal" href="gcsupport.html#c.PyObject_GC_Del" title="PyObject_GC_Del"><code class="xref c c-func docutils literal"><span class="pre">PyObject_GC_Del()</span></code></a> if the instance was allocated using
324
<a class="reference internal" href="gcsupport.html#c.PyObject_GC_New" title="PyObject_GC_New"><code class="xref c c-func docutils literal"><span class="pre">PyObject_GC_New()</span></code></a> or <a class="reference internal" href="gcsupport.html#c.PyObject_GC_NewVar" title="PyObject_GC_NewVar"><code class="xref c c-func docutils literal"><span class="pre">PyObject_GC_NewVar()</span></code></a>.</p>
325
<p>This field is inherited by subtypes.</p>
329
<dt id="c.PyTypeObject.tp_print">
330
printfunc <code class="descname">PyTypeObject.tp_print</code><a class="headerlink" href="#c.PyTypeObject.tp_print" title="Permalink to this definition">¶</a></dt>
331
<dd><p>Reserved slot, formerly used for print formatting in Python 2.x.</p>
335
<dt id="c.PyTypeObject.tp_getattr">
336
getattrfunc <code class="descname">PyTypeObject.tp_getattr</code><a class="headerlink" href="#c.PyTypeObject.tp_getattr" title="Permalink to this definition">¶</a></dt>
337
<dd><p>An optional pointer to the get-attribute-string function.</p>
338
<p>This field is deprecated. When it is defined, it should point to a function
339
that acts the same as the <a class="reference internal" href="#c.PyTypeObject.tp_getattro" title="PyTypeObject.tp_getattro"><code class="xref c c-member docutils literal"><span class="pre">tp_getattro</span></code></a> function, but taking a C string
340
instead of a Python string object to give the attribute name. The signature is
341
the same as for <a class="reference internal" href="object.html#c.PyObject_GetAttrString" title="PyObject_GetAttrString"><code class="xref c c-func docutils literal"><span class="pre">PyObject_GetAttrString()</span></code></a>.</p>
342
<p>This field is inherited by subtypes together with <a class="reference internal" href="#c.PyTypeObject.tp_getattro" title="PyTypeObject.tp_getattro"><code class="xref c c-member docutils literal"><span class="pre">tp_getattro</span></code></a>: a subtype
343
inherits both <a class="reference internal" href="#c.PyTypeObject.tp_getattr" title="PyTypeObject.tp_getattr"><code class="xref c c-member docutils literal"><span class="pre">tp_getattr</span></code></a> and <a class="reference internal" href="#c.PyTypeObject.tp_getattro" title="PyTypeObject.tp_getattro"><code class="xref c c-member docutils literal"><span class="pre">tp_getattro</span></code></a> from its base type when
344
the subtype’s <a class="reference internal" href="#c.PyTypeObject.tp_getattr" title="PyTypeObject.tp_getattr"><code class="xref c c-member docutils literal"><span class="pre">tp_getattr</span></code></a> and <a class="reference internal" href="#c.PyTypeObject.tp_getattro" title="PyTypeObject.tp_getattro"><code class="xref c c-member docutils literal"><span class="pre">tp_getattro</span></code></a> are both <em>NULL</em>.</p>
348
<dt id="c.PyTypeObject.tp_setattr">
349
setattrfunc <code class="descname">PyTypeObject.tp_setattr</code><a class="headerlink" href="#c.PyTypeObject.tp_setattr" title="Permalink to this definition">¶</a></dt>
350
<dd><p>An optional pointer to the function for setting and deleting attributes.</p>
351
<p>This field is deprecated. When it is defined, it should point to a function
352
that acts the same as the <a class="reference internal" href="#c.PyTypeObject.tp_setattro" title="PyTypeObject.tp_setattro"><code class="xref c c-member docutils literal"><span class="pre">tp_setattro</span></code></a> function, but taking a C string
353
instead of a Python string object to give the attribute name. The signature is
354
the same as for <a class="reference internal" href="object.html#c.PyObject_SetAttrString" title="PyObject_SetAttrString"><code class="xref c c-func docutils literal"><span class="pre">PyObject_SetAttrString()</span></code></a>, but setting
355
<em>v</em> to <em>NULL</em> to delete an attribute must be supported.</p>
356
<p>This field is inherited by subtypes together with <a class="reference internal" href="#c.PyTypeObject.tp_setattro" title="PyTypeObject.tp_setattro"><code class="xref c c-member docutils literal"><span class="pre">tp_setattro</span></code></a>: a subtype
357
inherits both <a class="reference internal" href="#c.PyTypeObject.tp_setattr" title="PyTypeObject.tp_setattr"><code class="xref c c-member docutils literal"><span class="pre">tp_setattr</span></code></a> and <a class="reference internal" href="#c.PyTypeObject.tp_setattro" title="PyTypeObject.tp_setattro"><code class="xref c c-member docutils literal"><span class="pre">tp_setattro</span></code></a> from its base type when
358
the subtype’s <a class="reference internal" href="#c.PyTypeObject.tp_setattr" title="PyTypeObject.tp_setattr"><code class="xref c c-member docutils literal"><span class="pre">tp_setattr</span></code></a> and <a class="reference internal" href="#c.PyTypeObject.tp_setattro" title="PyTypeObject.tp_setattro"><code class="xref c c-member docutils literal"><span class="pre">tp_setattro</span></code></a> are both <em>NULL</em>.</p>
362
<dt id="c.tp_as_async">
363
<a class="reference internal" href="#c.PyAsyncMethods" title="PyAsyncMethods">PyAsyncMethods</a>* <code class="descname">tp_as_async</code><a class="headerlink" href="#c.tp_as_async" title="Permalink to this definition">¶</a></dt>
364
<dd><p>Pointer to an additional structure that contains fields relevant only to
365
objects which implement <a class="reference internal" href="../glossary.html#term-awaitable"><span class="xref std std-term">awaitable</span></a> and <a class="reference internal" href="../glossary.html#term-asynchronous-iterator"><span class="xref std std-term">asynchronous iterator</span></a>
366
protocols at the C-level. See <a class="reference internal" href="#async-structs"><span>Async Object Structures</span></a> for details.</p>
367
<div class="versionadded">
368
<p><span class="versionmodified">New in version 3.5: </span>Formerly known as <code class="docutils literal"><span class="pre">tp_compare</span></code> and <code class="docutils literal"><span class="pre">tp_reserved</span></code>.</p>
373
<dt id="c.PyTypeObject.tp_repr">
374
reprfunc <code class="descname">PyTypeObject.tp_repr</code><a class="headerlink" href="#c.PyTypeObject.tp_repr" title="Permalink to this definition">¶</a></dt>
375
<dd><p id="index-1">An optional pointer to a function that implements the built-in function
376
<a class="reference internal" href="../library/functions.html#repr" title="repr"><code class="xref py py-func docutils literal"><span class="pre">repr()</span></code></a>.</p>
377
<p>The signature is the same as for <a class="reference internal" href="object.html#c.PyObject_Repr" title="PyObject_Repr"><code class="xref c c-func docutils literal"><span class="pre">PyObject_Repr()</span></code></a>; it must return a string
378
or a Unicode object. Ideally, this function should return a string that, when
379
passed to <a class="reference internal" href="../library/functions.html#eval" title="eval"><code class="xref py py-func docutils literal"><span class="pre">eval()</span></code></a>, given a suitable environment, returns an object with the
380
same value. If this is not feasible, it should return a string starting with
381
<code class="docutils literal"><span class="pre">'<'</span></code> and ending with <code class="docutils literal"><span class="pre">'>'</span></code> from which both the type and the value of the
382
object can be deduced.</p>
383
<p>When this field is not set, a string of the form <code class="docutils literal"><span class="pre"><%s</span> <span class="pre">object</span> <span class="pre">at</span> <span class="pre">%p></span></code> is
384
returned, where <code class="docutils literal"><span class="pre">%s</span></code> is replaced by the type name, and <code class="docutils literal"><span class="pre">%p</span></code> by the object’s
386
<p>This field is inherited by subtypes.</p>
390
<dt id="c.tp_as_number">
391
<a class="reference internal" href="#c.PyNumberMethods" title="PyNumberMethods">PyNumberMethods</a>* <code class="descname">tp_as_number</code><a class="headerlink" href="#c.tp_as_number" title="Permalink to this definition">¶</a></dt>
392
<dd><p>Pointer to an additional structure that contains fields relevant only to
393
objects which implement the number protocol. These fields are documented in
394
<a class="reference internal" href="#number-structs"><span>Number Object Structures</span></a>.</p>
395
<p>The <code class="xref c c-member docutils literal"><span class="pre">tp_as_number</span></code> field is not inherited, but the contained fields are
396
inherited individually.</p>
400
<dt id="c.tp_as_sequence">
401
<a class="reference internal" href="#c.PySequenceMethods" title="PySequenceMethods">PySequenceMethods</a>* <code class="descname">tp_as_sequence</code><a class="headerlink" href="#c.tp_as_sequence" title="Permalink to this definition">¶</a></dt>
402
<dd><p>Pointer to an additional structure that contains fields relevant only to
403
objects which implement the sequence protocol. These fields are documented
404
in <a class="reference internal" href="#sequence-structs"><span>Sequence Object Structures</span></a>.</p>
405
<p>The <code class="xref c c-member docutils literal"><span class="pre">tp_as_sequence</span></code> field is not inherited, but the contained fields
406
are inherited individually.</p>
410
<dt id="c.tp_as_mapping">
411
<a class="reference internal" href="#c.PyMappingMethods" title="PyMappingMethods">PyMappingMethods</a>* <code class="descname">tp_as_mapping</code><a class="headerlink" href="#c.tp_as_mapping" title="Permalink to this definition">¶</a></dt>
412
<dd><p>Pointer to an additional structure that contains fields relevant only to
413
objects which implement the mapping protocol. These fields are documented in
414
<a class="reference internal" href="#mapping-structs"><span>Mapping Object Structures</span></a>.</p>
415
<p>The <code class="xref c c-member docutils literal"><span class="pre">tp_as_mapping</span></code> field is not inherited, but the contained fields
416
are inherited individually.</p>
420
<dt id="c.PyTypeObject.tp_hash">
421
hashfunc <code class="descname">PyTypeObject.tp_hash</code><a class="headerlink" href="#c.PyTypeObject.tp_hash" title="Permalink to this definition">¶</a></dt>
422
<dd><p id="index-2">An optional pointer to a function that implements the built-in function
423
<a class="reference internal" href="../library/functions.html#hash" title="hash"><code class="xref py py-func docutils literal"><span class="pre">hash()</span></code></a>.</p>
424
<p>The signature is the same as for <a class="reference internal" href="object.html#c.PyObject_Hash" title="PyObject_Hash"><code class="xref c c-func docutils literal"><span class="pre">PyObject_Hash()</span></code></a>; it must return a
425
value of the type Py_hash_t. The value <code class="docutils literal"><span class="pre">-1</span></code> should not be returned as a
426
normal return value; when an error occurs during the computation of the hash
427
value, the function should set an exception and return <code class="docutils literal"><span class="pre">-1</span></code>.</p>
428
<p>This field can be set explicitly to <a class="reference internal" href="object.html#c.PyObject_HashNotImplemented" title="PyObject_HashNotImplemented"><code class="xref c c-func docutils literal"><span class="pre">PyObject_HashNotImplemented()</span></code></a> to
429
block inheritance of the hash method from a parent type. This is interpreted
430
as the equivalent of <code class="docutils literal"><span class="pre">__hash__</span> <span class="pre">=</span> <span class="pre">None</span></code> at the Python level, causing
431
<code class="docutils literal"><span class="pre">isinstance(o,</span> <span class="pre">collections.Hashable)</span></code> to correctly return <code class="docutils literal"><span class="pre">False</span></code>. Note
432
that the converse is also true - setting <code class="docutils literal"><span class="pre">__hash__</span> <span class="pre">=</span> <span class="pre">None</span></code> on a class at
433
the Python level will result in the <code class="docutils literal"><span class="pre">tp_hash</span></code> slot being set to
434
<a class="reference internal" href="object.html#c.PyObject_HashNotImplemented" title="PyObject_HashNotImplemented"><code class="xref c c-func docutils literal"><span class="pre">PyObject_HashNotImplemented()</span></code></a>.</p>
435
<p>When this field is not set, an attempt to take the hash of the
436
object raises <a class="reference internal" href="../library/exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal"><span class="pre">TypeError</span></code></a>.</p>
437
<p>This field is inherited by subtypes together with
438
<a class="reference internal" href="#c.PyTypeObject.tp_richcompare" title="PyTypeObject.tp_richcompare"><code class="xref c c-member docutils literal"><span class="pre">tp_richcompare</span></code></a>: a subtype inherits both of
439
<a class="reference internal" href="#c.PyTypeObject.tp_richcompare" title="PyTypeObject.tp_richcompare"><code class="xref c c-member docutils literal"><span class="pre">tp_richcompare</span></code></a> and <a class="reference internal" href="#c.PyTypeObject.tp_hash" title="PyTypeObject.tp_hash"><code class="xref c c-member docutils literal"><span class="pre">tp_hash</span></code></a>, when the subtype’s
440
<a class="reference internal" href="#c.PyTypeObject.tp_richcompare" title="PyTypeObject.tp_richcompare"><code class="xref c c-member docutils literal"><span class="pre">tp_richcompare</span></code></a> and <a class="reference internal" href="#c.PyTypeObject.tp_hash" title="PyTypeObject.tp_hash"><code class="xref c c-member docutils literal"><span class="pre">tp_hash</span></code></a> are both <em>NULL</em>.</p>
444
<dt id="c.PyTypeObject.tp_call">
445
ternaryfunc <code class="descname">PyTypeObject.tp_call</code><a class="headerlink" href="#c.PyTypeObject.tp_call" title="Permalink to this definition">¶</a></dt>
446
<dd><p>An optional pointer to a function that implements calling the object. This
447
should be <em>NULL</em> if the object is not callable. The signature is the same as
448
for <a class="reference internal" href="object.html#c.PyObject_Call" title="PyObject_Call"><code class="xref c c-func docutils literal"><span class="pre">PyObject_Call()</span></code></a>.</p>
449
<p>This field is inherited by subtypes.</p>
453
<dt id="c.PyTypeObject.tp_str">
454
reprfunc <code class="descname">PyTypeObject.tp_str</code><a class="headerlink" href="#c.PyTypeObject.tp_str" title="Permalink to this definition">¶</a></dt>
455
<dd><p>An optional pointer to a function that implements the built-in operation
456
<a class="reference internal" href="../library/stdtypes.html#str" title="str"><code class="xref py py-func docutils literal"><span class="pre">str()</span></code></a>. (Note that <a class="reference internal" href="../library/stdtypes.html#str" title="str"><code class="xref py py-class docutils literal"><span class="pre">str</span></code></a> is a type now, and <a class="reference internal" href="../library/stdtypes.html#str" title="str"><code class="xref py py-func docutils literal"><span class="pre">str()</span></code></a> calls the
457
constructor for that type. This constructor calls <a class="reference internal" href="object.html#c.PyObject_Str" title="PyObject_Str"><code class="xref c c-func docutils literal"><span class="pre">PyObject_Str()</span></code></a> to do
458
the actual work, and <a class="reference internal" href="object.html#c.PyObject_Str" title="PyObject_Str"><code class="xref c c-func docutils literal"><span class="pre">PyObject_Str()</span></code></a> will call this handler.)</p>
459
<p>The signature is the same as for <a class="reference internal" href="object.html#c.PyObject_Str" title="PyObject_Str"><code class="xref c c-func docutils literal"><span class="pre">PyObject_Str()</span></code></a>; it must return a string
460
or a Unicode object. This function should return a “friendly” string
461
representation of the object, as this is the representation that will be used,
462
among other things, by the <a class="reference internal" href="../library/functions.html#print" title="print"><code class="xref py py-func docutils literal"><span class="pre">print()</span></code></a> function.</p>
463
<p>When this field is not set, <a class="reference internal" href="object.html#c.PyObject_Repr" title="PyObject_Repr"><code class="xref c c-func docutils literal"><span class="pre">PyObject_Repr()</span></code></a> is called to return a string
465
<p>This field is inherited by subtypes.</p>
469
<dt id="c.PyTypeObject.tp_getattro">
470
getattrofunc <code class="descname">PyTypeObject.tp_getattro</code><a class="headerlink" href="#c.PyTypeObject.tp_getattro" title="Permalink to this definition">¶</a></dt>
471
<dd><p>An optional pointer to the get-attribute function.</p>
472
<p>The signature is the same as for <a class="reference internal" href="object.html#c.PyObject_GetAttr" title="PyObject_GetAttr"><code class="xref c c-func docutils literal"><span class="pre">PyObject_GetAttr()</span></code></a>. It is usually
473
convenient to set this field to <a class="reference internal" href="object.html#c.PyObject_GenericGetAttr" title="PyObject_GenericGetAttr"><code class="xref c c-func docutils literal"><span class="pre">PyObject_GenericGetAttr()</span></code></a>, which
474
implements the normal way of looking for object attributes.</p>
475
<p>This field is inherited by subtypes together with <a class="reference internal" href="#c.PyTypeObject.tp_getattr" title="PyTypeObject.tp_getattr"><code class="xref c c-member docutils literal"><span class="pre">tp_getattr</span></code></a>: a subtype
476
inherits both <a class="reference internal" href="#c.PyTypeObject.tp_getattr" title="PyTypeObject.tp_getattr"><code class="xref c c-member docutils literal"><span class="pre">tp_getattr</span></code></a> and <a class="reference internal" href="#c.PyTypeObject.tp_getattro" title="PyTypeObject.tp_getattro"><code class="xref c c-member docutils literal"><span class="pre">tp_getattro</span></code></a> from its base type when
477
the subtype’s <a class="reference internal" href="#c.PyTypeObject.tp_getattr" title="PyTypeObject.tp_getattr"><code class="xref c c-member docutils literal"><span class="pre">tp_getattr</span></code></a> and <a class="reference internal" href="#c.PyTypeObject.tp_getattro" title="PyTypeObject.tp_getattro"><code class="xref c c-member docutils literal"><span class="pre">tp_getattro</span></code></a> are both <em>NULL</em>.</p>
481
<dt id="c.PyTypeObject.tp_setattro">
482
setattrofunc <code class="descname">PyTypeObject.tp_setattro</code><a class="headerlink" href="#c.PyTypeObject.tp_setattro" title="Permalink to this definition">¶</a></dt>
483
<dd><p>An optional pointer to the function for setting and deleting attributes.</p>
484
<p>The signature is the same as for <a class="reference internal" href="object.html#c.PyObject_SetAttr" title="PyObject_SetAttr"><code class="xref c c-func docutils literal"><span class="pre">PyObject_SetAttr()</span></code></a>, but setting
485
<em>v</em> to <em>NULL</em> to delete an attribute must be supported. It is usually
486
convenient to set this field to <a class="reference internal" href="object.html#c.PyObject_GenericSetAttr" title="PyObject_GenericSetAttr"><code class="xref c c-func docutils literal"><span class="pre">PyObject_GenericSetAttr()</span></code></a>, which
487
implements the normal way of setting object attributes.</p>
488
<p>This field is inherited by subtypes together with <a class="reference internal" href="#c.PyTypeObject.tp_setattr" title="PyTypeObject.tp_setattr"><code class="xref c c-member docutils literal"><span class="pre">tp_setattr</span></code></a>: a subtype
489
inherits both <a class="reference internal" href="#c.PyTypeObject.tp_setattr" title="PyTypeObject.tp_setattr"><code class="xref c c-member docutils literal"><span class="pre">tp_setattr</span></code></a> and <a class="reference internal" href="#c.PyTypeObject.tp_setattro" title="PyTypeObject.tp_setattro"><code class="xref c c-member docutils literal"><span class="pre">tp_setattro</span></code></a> from its base type when
490
the subtype’s <a class="reference internal" href="#c.PyTypeObject.tp_setattr" title="PyTypeObject.tp_setattr"><code class="xref c c-member docutils literal"><span class="pre">tp_setattr</span></code></a> and <a class="reference internal" href="#c.PyTypeObject.tp_setattro" title="PyTypeObject.tp_setattro"><code class="xref c c-member docutils literal"><span class="pre">tp_setattro</span></code></a> are both <em>NULL</em>.</p>
494
<dt id="c.PyTypeObject.tp_as_buffer">
495
<a class="reference internal" href="#c.PyBufferProcs" title="PyBufferProcs">PyBufferProcs</a>* <code class="descname">PyTypeObject.tp_as_buffer</code><a class="headerlink" href="#c.PyTypeObject.tp_as_buffer" title="Permalink to this definition">¶</a></dt>
496
<dd><p>Pointer to an additional structure that contains fields relevant only to objects
497
which implement the buffer interface. These fields are documented in
498
<a class="reference internal" href="#buffer-structs"><span>Buffer Object Structures</span></a>.</p>
499
<p>The <a class="reference internal" href="#c.PyTypeObject.tp_as_buffer" title="PyTypeObject.tp_as_buffer"><code class="xref c c-member docutils literal"><span class="pre">tp_as_buffer</span></code></a> field is not inherited, but the contained fields are
500
inherited individually.</p>
504
<dt id="c.PyTypeObject.tp_flags">
505
unsigned long <code class="descname">PyTypeObject.tp_flags</code><a class="headerlink" href="#c.PyTypeObject.tp_flags" title="Permalink to this definition">¶</a></dt>
506
<dd><p>This field is a bit mask of various flags. Some flags indicate variant
507
semantics for certain situations; others are used to indicate that certain
508
fields in the type object (or in the extension structures referenced via
509
<code class="xref c c-member docutils literal"><span class="pre">tp_as_number</span></code>, <code class="xref c c-member docutils literal"><span class="pre">tp_as_sequence</span></code>, <code class="xref c c-member docutils literal"><span class="pre">tp_as_mapping</span></code>, and
510
<a class="reference internal" href="#c.PyTypeObject.tp_as_buffer" title="PyTypeObject.tp_as_buffer"><code class="xref c c-member docutils literal"><span class="pre">tp_as_buffer</span></code></a>) that were historically not always present are valid; if
511
such a flag bit is clear, the type fields it guards must not be accessed and
512
must be considered to have a zero or <em>NULL</em> value instead.</p>
513
<p>Inheritance of this field is complicated. Most flag bits are inherited
514
individually, i.e. if the base type has a flag bit set, the subtype inherits
515
this flag bit. The flag bits that pertain to extension structures are strictly
516
inherited if the extension structure is inherited, i.e. the base type’s value of
517
the flag bit is copied into the subtype together with a pointer to the extension
518
structure. The <a class="reference internal" href="#Py_TPFLAGS_HAVE_GC" title="Py_TPFLAGS_HAVE_GC"><code class="xref py py-const docutils literal"><span class="pre">Py_TPFLAGS_HAVE_GC</span></code></a> flag bit is inherited together with
519
the <a class="reference internal" href="#c.PyTypeObject.tp_traverse" title="PyTypeObject.tp_traverse"><code class="xref c c-member docutils literal"><span class="pre">tp_traverse</span></code></a> and <a class="reference internal" href="#c.PyTypeObject.tp_clear" title="PyTypeObject.tp_clear"><code class="xref c c-member docutils literal"><span class="pre">tp_clear</span></code></a> fields, i.e. if the
520
<a class="reference internal" href="#Py_TPFLAGS_HAVE_GC" title="Py_TPFLAGS_HAVE_GC"><code class="xref py py-const docutils literal"><span class="pre">Py_TPFLAGS_HAVE_GC</span></code></a> flag bit is clear in the subtype and the
521
<a class="reference internal" href="#c.PyTypeObject.tp_traverse" title="PyTypeObject.tp_traverse"><code class="xref c c-member docutils literal"><span class="pre">tp_traverse</span></code></a> and <a class="reference internal" href="#c.PyTypeObject.tp_clear" title="PyTypeObject.tp_clear"><code class="xref c c-member docutils literal"><span class="pre">tp_clear</span></code></a> fields in the subtype exist and have
522
<em>NULL</em> values.</p>
523
<p>The following bit masks are currently defined; these can be ORed together using
524
the <code class="docutils literal"><span class="pre">|</span></code> operator to form the value of the <a class="reference internal" href="#c.PyTypeObject.tp_flags" title="PyTypeObject.tp_flags"><code class="xref c c-member docutils literal"><span class="pre">tp_flags</span></code></a> field. The macro
525
<a class="reference internal" href="type.html#c.PyType_HasFeature" title="PyType_HasFeature"><code class="xref c c-func docutils literal"><span class="pre">PyType_HasFeature()</span></code></a> takes a type and a flags value, <em>tp</em> and <em>f</em>, and
526
checks whether <code class="docutils literal"><span class="pre">tp->tp_flags</span> <span class="pre">&</span> <span class="pre">f</span></code> is non-zero.</p>
528
<dt id="Py_TPFLAGS_HEAPTYPE">
529
<code class="descname">Py_TPFLAGS_HEAPTYPE</code><a class="headerlink" href="#Py_TPFLAGS_HEAPTYPE" title="Permalink to this definition">¶</a></dt>
530
<dd><p>This bit is set when the type object itself is allocated on the heap. In this
531
case, the <code class="xref py py-attr docutils literal"><span class="pre">ob_type</span></code> field of its instances is considered a reference to
532
the type, and the type object is INCREF’ed when a new instance is created, and
533
DECREF’ed when an instance is destroyed (this does not apply to instances of
534
subtypes; only the type referenced by the instance’s ob_type gets INCREF’ed or
535
DECREF’ed).</p>
539
<dt id="Py_TPFLAGS_BASETYPE">
540
<code class="descname">Py_TPFLAGS_BASETYPE</code><a class="headerlink" href="#Py_TPFLAGS_BASETYPE" title="Permalink to this definition">¶</a></dt>
541
<dd><p>This bit is set when the type can be used as the base type of another type. If
542
this bit is clear, the type cannot be subtyped (similar to a “final” class in
547
<dt id="Py_TPFLAGS_READY">
548
<code class="descname">Py_TPFLAGS_READY</code><a class="headerlink" href="#Py_TPFLAGS_READY" title="Permalink to this definition">¶</a></dt>
549
<dd><p>This bit is set when the type object has been fully initialized by
550
<a class="reference internal" href="type.html#c.PyType_Ready" title="PyType_Ready"><code class="xref c c-func docutils literal"><span class="pre">PyType_Ready()</span></code></a>.</p>
554
<dt id="Py_TPFLAGS_READYING">
555
<code class="descname">Py_TPFLAGS_READYING</code><a class="headerlink" href="#Py_TPFLAGS_READYING" title="Permalink to this definition">¶</a></dt>
556
<dd><p>This bit is set while <a class="reference internal" href="type.html#c.PyType_Ready" title="PyType_Ready"><code class="xref c c-func docutils literal"><span class="pre">PyType_Ready()</span></code></a> is in the process of initializing
561
<dt id="Py_TPFLAGS_HAVE_GC">
562
<code class="descname">Py_TPFLAGS_HAVE_GC</code><a class="headerlink" href="#Py_TPFLAGS_HAVE_GC" title="Permalink to this definition">¶</a></dt>
563
<dd><p>This bit is set when the object supports garbage collection. If this bit
564
is set, instances must be created using <a class="reference internal" href="gcsupport.html#c.PyObject_GC_New" title="PyObject_GC_New"><code class="xref c c-func docutils literal"><span class="pre">PyObject_GC_New()</span></code></a> and
565
destroyed using <a class="reference internal" href="gcsupport.html#c.PyObject_GC_Del" title="PyObject_GC_Del"><code class="xref c c-func docutils literal"><span class="pre">PyObject_GC_Del()</span></code></a>. More information in section
566
<a class="reference internal" href="gcsupport.html#supporting-cycle-detection"><span>Supporting Cyclic Garbage Collection</span></a>. This bit also implies that the
567
GC-related fields <a class="reference internal" href="#c.PyTypeObject.tp_traverse" title="PyTypeObject.tp_traverse"><code class="xref c c-member docutils literal"><span class="pre">tp_traverse</span></code></a> and <a class="reference internal" href="#c.PyTypeObject.tp_clear" title="PyTypeObject.tp_clear"><code class="xref c c-member docutils literal"><span class="pre">tp_clear</span></code></a> are present in
572
<dt id="Py_TPFLAGS_DEFAULT">
573
<code class="descname">Py_TPFLAGS_DEFAULT</code><a class="headerlink" href="#Py_TPFLAGS_DEFAULT" title="Permalink to this definition">¶</a></dt>
574
<dd><p>This is a bitmask of all the bits that pertain to the existence of certain
575
fields in the type object and its extension structures. Currently, it includes
576
the following bits: <code class="xref py py-const docutils literal"><span class="pre">Py_TPFLAGS_HAVE_STACKLESS_EXTENSION</span></code>,
577
<code class="xref py py-const docutils literal"><span class="pre">Py_TPFLAGS_HAVE_VERSION_TAG</span></code>.</p>
581
<dt id="Py_TPFLAGS_LONG_SUBCLASS">
582
<code class="descname">Py_TPFLAGS_LONG_SUBCLASS</code><a class="headerlink" href="#Py_TPFLAGS_LONG_SUBCLASS" title="Permalink to this definition">¶</a></dt>
586
<dt id="Py_TPFLAGS_LIST_SUBCLASS">
587
<code class="descname">Py_TPFLAGS_LIST_SUBCLASS</code><a class="headerlink" href="#Py_TPFLAGS_LIST_SUBCLASS" title="Permalink to this definition">¶</a></dt>
591
<dt id="Py_TPFLAGS_TUPLE_SUBCLASS">
592
<code class="descname">Py_TPFLAGS_TUPLE_SUBCLASS</code><a class="headerlink" href="#Py_TPFLAGS_TUPLE_SUBCLASS" title="Permalink to this definition">¶</a></dt>
596
<dt id="Py_TPFLAGS_BYTES_SUBCLASS">
597
<code class="descname">Py_TPFLAGS_BYTES_SUBCLASS</code><a class="headerlink" href="#Py_TPFLAGS_BYTES_SUBCLASS" title="Permalink to this definition">¶</a></dt>
601
<dt id="Py_TPFLAGS_UNICODE_SUBCLASS">
602
<code class="descname">Py_TPFLAGS_UNICODE_SUBCLASS</code><a class="headerlink" href="#Py_TPFLAGS_UNICODE_SUBCLASS" title="Permalink to this definition">¶</a></dt>
606
<dt id="Py_TPFLAGS_DICT_SUBCLASS">
607
<code class="descname">Py_TPFLAGS_DICT_SUBCLASS</code><a class="headerlink" href="#Py_TPFLAGS_DICT_SUBCLASS" title="Permalink to this definition">¶</a></dt>
611
<dt id="Py_TPFLAGS_BASE_EXC_SUBCLASS">
612
<code class="descname">Py_TPFLAGS_BASE_EXC_SUBCLASS</code><a class="headerlink" href="#Py_TPFLAGS_BASE_EXC_SUBCLASS" title="Permalink to this definition">¶</a></dt>
616
<dt id="Py_TPFLAGS_TYPE_SUBCLASS">
617
<code class="descname">Py_TPFLAGS_TYPE_SUBCLASS</code><a class="headerlink" href="#Py_TPFLAGS_TYPE_SUBCLASS" title="Permalink to this definition">¶</a></dt>
618
<dd><p>These flags are used by functions such as
619
<a class="reference internal" href="long.html#c.PyLong_Check" title="PyLong_Check"><code class="xref c c-func docutils literal"><span class="pre">PyLong_Check()</span></code></a> to quickly determine if a type is a subclass
620
of a built-in type; such specific checks are faster than a generic
621
check, like <a class="reference internal" href="object.html#c.PyObject_IsInstance" title="PyObject_IsInstance"><code class="xref c c-func docutils literal"><span class="pre">PyObject_IsInstance()</span></code></a>. Custom types that inherit
622
from built-ins should have their <a class="reference internal" href="#c.PyTypeObject.tp_flags" title="PyTypeObject.tp_flags"><code class="xref c c-member docutils literal"><span class="pre">tp_flags</span></code></a>
623
set appropriately, or the code that interacts with such types
624
will behave differently depending on what kind of check is used.</p>
628
<dt id="Py_TPFLAGS_HAVE_FINALIZE">
629
<code class="descname">Py_TPFLAGS_HAVE_FINALIZE</code><a class="headerlink" href="#Py_TPFLAGS_HAVE_FINALIZE" title="Permalink to this definition">¶</a></dt>
630
<dd><p>This bit is set when the <a class="reference internal" href="#c.PyTypeObject.tp_finalize" title="PyTypeObject.tp_finalize"><code class="xref c c-member docutils literal"><span class="pre">tp_finalize</span></code></a> slot is present in the
632
<div class="versionadded">
633
<p><span class="versionmodified">New in version 3.4.</span></p>
640
<dt id="c.PyTypeObject.tp_doc">
641
const char* <code class="descname">PyTypeObject.tp_doc</code><a class="headerlink" href="#c.PyTypeObject.tp_doc" title="Permalink to this definition">¶</a></dt>
642
<dd><p>An optional pointer to a NUL-terminated C string giving the docstring for this
643
type object. This is exposed as the <code class="xref py py-attr docutils literal"><span class="pre">__doc__</span></code> attribute on the type and
644
instances of the type.</p>
645
<p>This field is <em>not</em> inherited by subtypes.</p>
649
<dt id="c.PyTypeObject.tp_traverse">
650
<a class="reference internal" href="gcsupport.html#c.traverseproc" title="traverseproc">traverseproc</a> <code class="descname">PyTypeObject.tp_traverse</code><a class="headerlink" href="#c.PyTypeObject.tp_traverse" title="Permalink to this definition">¶</a></dt>
651
<dd><p>An optional pointer to a traversal function for the garbage collector. This is
652
only used if the <a class="reference internal" href="#Py_TPFLAGS_HAVE_GC" title="Py_TPFLAGS_HAVE_GC"><code class="xref py py-const docutils literal"><span class="pre">Py_TPFLAGS_HAVE_GC</span></code></a> flag bit is set. More information
653
about Python’s garbage collection scheme can be found in section
654
<a class="reference internal" href="gcsupport.html#supporting-cycle-detection"><span>Supporting Cyclic Garbage Collection</span></a>.</p>
655
<p>The <a class="reference internal" href="#c.PyTypeObject.tp_traverse" title="PyTypeObject.tp_traverse"><code class="xref c c-member docutils literal"><span class="pre">tp_traverse</span></code></a> pointer is used by the garbage collector to detect
656
reference cycles. A typical implementation of a <a class="reference internal" href="#c.PyTypeObject.tp_traverse" title="PyTypeObject.tp_traverse"><code class="xref c c-member docutils literal"><span class="pre">tp_traverse</span></code></a> function
657
simply calls <a class="reference internal" href="gcsupport.html#c.Py_VISIT" title="Py_VISIT"><code class="xref c c-func docutils literal"><span class="pre">Py_VISIT()</span></code></a> on each of the instance’s members that are Python
658
objects. For example, this is function <code class="xref c c-func docutils literal"><span class="pre">local_traverse()</span></code> from the
659
<a class="reference internal" href="../library/_thread.html#module-_thread" title="_thread: Low-level threading API."><code class="xref py py-mod docutils literal"><span class="pre">_thread</span></code></a> extension module:</p>
660
<div class="highlight-c"><div class="highlight"><pre><span class="k">static</span> <span class="kt">int</span>
661
<span class="nf">local_traverse</span><span class="p">(</span><span class="n">localobject</span> <span class="o">*</span><span class="n">self</span><span class="p">,</span> <span class="n">visitproc</span> <span class="n">visit</span><span class="p">,</span> <span class="kt">void</span> <span class="o">*</span><span class="n">arg</span><span class="p">)</span>
662
<span class="p">{</span>
663
<span class="n">Py_VISIT</span><span class="p">(</span><span class="n">self</span><span class="o">-></span><span class="n">args</span><span class="p">);</span>
664
<span class="n">Py_VISIT</span><span class="p">(</span><span class="n">self</span><span class="o">-></span><span class="n">kw</span><span class="p">);</span>
665
<span class="n">Py_VISIT</span><span class="p">(</span><span class="n">self</span><span class="o">-></span><span class="n">dict</span><span class="p">);</span>
666
<span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
667
<span class="p">}</span>
670
<p>Note that <a class="reference internal" href="gcsupport.html#c.Py_VISIT" title="Py_VISIT"><code class="xref c c-func docutils literal"><span class="pre">Py_VISIT()</span></code></a> is called only on those members that can participate
671
in reference cycles. Although there is also a <code class="docutils literal"><span class="pre">self->key</span></code> member, it can only
672
be <em>NULL</em> or a Python string and therefore cannot be part of a reference cycle.</p>
673
<p>On the other hand, even if you know a member can never be part of a cycle, as a
674
debugging aid you may want to visit it anyway just so the <a class="reference internal" href="../library/gc.html#module-gc" title="gc: Interface to the cycle-detecting garbage collector."><code class="xref py py-mod docutils literal"><span class="pre">gc</span></code></a> module’s
675
<a class="reference internal" href="../library/gc.html#gc.get_referents" title="gc.get_referents"><code class="xref py py-func docutils literal"><span class="pre">get_referents()</span></code></a> function will include it.</p>
676
<p>Note that <a class="reference internal" href="gcsupport.html#c.Py_VISIT" title="Py_VISIT"><code class="xref c c-func docutils literal"><span class="pre">Py_VISIT()</span></code></a> requires the <em>visit</em> and <em>arg</em> parameters to
677
<code class="xref c c-func docutils literal"><span class="pre">local_traverse()</span></code> to have these specific names; don’t name them just
679
<p>This field is inherited by subtypes together with <a class="reference internal" href="#c.PyTypeObject.tp_clear" title="PyTypeObject.tp_clear"><code class="xref c c-member docutils literal"><span class="pre">tp_clear</span></code></a> and the
680
<a class="reference internal" href="#Py_TPFLAGS_HAVE_GC" title="Py_TPFLAGS_HAVE_GC"><code class="xref py py-const docutils literal"><span class="pre">Py_TPFLAGS_HAVE_GC</span></code></a> flag bit: the flag bit, <a class="reference internal" href="#c.PyTypeObject.tp_traverse" title="PyTypeObject.tp_traverse"><code class="xref c c-member docutils literal"><span class="pre">tp_traverse</span></code></a>, and
681
<a class="reference internal" href="#c.PyTypeObject.tp_clear" title="PyTypeObject.tp_clear"><code class="xref c c-member docutils literal"><span class="pre">tp_clear</span></code></a> are all inherited from the base type if they are all zero in
686
<dt id="c.PyTypeObject.tp_clear">
687
<a class="reference internal" href="gcsupport.html#c.inquiry" title="inquiry">inquiry</a> <code class="descname">PyTypeObject.tp_clear</code><a class="headerlink" href="#c.PyTypeObject.tp_clear" title="Permalink to this definition">¶</a></dt>
688
<dd><p>An optional pointer to a clear function for the garbage collector. This is only
689
used if the <a class="reference internal" href="#Py_TPFLAGS_HAVE_GC" title="Py_TPFLAGS_HAVE_GC"><code class="xref py py-const docutils literal"><span class="pre">Py_TPFLAGS_HAVE_GC</span></code></a> flag bit is set.</p>
690
<p>The <a class="reference internal" href="#c.PyTypeObject.tp_clear" title="PyTypeObject.tp_clear"><code class="xref c c-member docutils literal"><span class="pre">tp_clear</span></code></a> member function is used to break reference cycles in cyclic
691
garbage detected by the garbage collector. Taken together, all <a class="reference internal" href="#c.PyTypeObject.tp_clear" title="PyTypeObject.tp_clear"><code class="xref c c-member docutils literal"><span class="pre">tp_clear</span></code></a>
692
functions in the system must combine to break all reference cycles. This is
693
subtle, and if in any doubt supply a <a class="reference internal" href="#c.PyTypeObject.tp_clear" title="PyTypeObject.tp_clear"><code class="xref c c-member docutils literal"><span class="pre">tp_clear</span></code></a> function. For example,
694
the tuple type does not implement a <a class="reference internal" href="#c.PyTypeObject.tp_clear" title="PyTypeObject.tp_clear"><code class="xref c c-member docutils literal"><span class="pre">tp_clear</span></code></a> function, because it’s
695
possible to prove that no reference cycle can be composed entirely of tuples.
696
Therefore the <a class="reference internal" href="#c.PyTypeObject.tp_clear" title="PyTypeObject.tp_clear"><code class="xref c c-member docutils literal"><span class="pre">tp_clear</span></code></a> functions of other types must be sufficient to
697
break any cycle containing a tuple. This isn’t immediately obvious, and there’s
698
rarely a good reason to avoid implementing <a class="reference internal" href="#c.PyTypeObject.tp_clear" title="PyTypeObject.tp_clear"><code class="xref c c-member docutils literal"><span class="pre">tp_clear</span></code></a>.</p>
699
<p>Implementations of <a class="reference internal" href="#c.PyTypeObject.tp_clear" title="PyTypeObject.tp_clear"><code class="xref c c-member docutils literal"><span class="pre">tp_clear</span></code></a> should drop the instance’s references to
700
those of its members that may be Python objects, and set its pointers to those
701
members to <em>NULL</em>, as in the following example:</p>
702
<div class="highlight-c"><div class="highlight"><pre><span class="k">static</span> <span class="kt">int</span>
703
<span class="nf">local_clear</span><span class="p">(</span><span class="n">localobject</span> <span class="o">*</span><span class="n">self</span><span class="p">)</span>
704
<span class="p">{</span>
705
<span class="n">Py_CLEAR</span><span class="p">(</span><span class="n">self</span><span class="o">-></span><span class="n">key</span><span class="p">);</span>
706
<span class="n">Py_CLEAR</span><span class="p">(</span><span class="n">self</span><span class="o">-></span><span class="n">args</span><span class="p">);</span>
707
<span class="n">Py_CLEAR</span><span class="p">(</span><span class="n">self</span><span class="o">-></span><span class="n">kw</span><span class="p">);</span>
708
<span class="n">Py_CLEAR</span><span class="p">(</span><span class="n">self</span><span class="o">-></span><span class="n">dict</span><span class="p">);</span>
709
<span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
710
<span class="p">}</span>
713
<p>The <a class="reference internal" href="refcounting.html#c.Py_CLEAR" title="Py_CLEAR"><code class="xref c c-func docutils literal"><span class="pre">Py_CLEAR()</span></code></a> macro should be used, because clearing references is
714
delicate: the reference to the contained object must not be decremented until
715
after the pointer to the contained object is set to <em>NULL</em>. This is because
716
decrementing the reference count may cause the contained object to become trash,
717
triggering a chain of reclamation activity that may include invoking arbitrary
718
Python code (due to finalizers, or weakref callbacks, associated with the
719
contained object). If it’s possible for such code to reference <em>self</em> again,
720
it’s important that the pointer to the contained object be <em>NULL</em> at that time,
721
so that <em>self</em> knows the contained object can no longer be used. The
722
<a class="reference internal" href="refcounting.html#c.Py_CLEAR" title="Py_CLEAR"><code class="xref c c-func docutils literal"><span class="pre">Py_CLEAR()</span></code></a> macro performs the operations in a safe order.</p>
723
<p>Because the goal of <a class="reference internal" href="#c.PyTypeObject.tp_clear" title="PyTypeObject.tp_clear"><code class="xref c c-member docutils literal"><span class="pre">tp_clear</span></code></a> functions is to break reference cycles,
724
it’s not necessary to clear contained objects like Python strings or Python
725
integers, which can’t participate in reference cycles. On the other hand, it may
726
be convenient to clear all contained Python objects, and write the type’s
727
<a class="reference internal" href="#c.PyTypeObject.tp_dealloc" title="PyTypeObject.tp_dealloc"><code class="xref c c-member docutils literal"><span class="pre">tp_dealloc</span></code></a> function to invoke <a class="reference internal" href="#c.PyTypeObject.tp_clear" title="PyTypeObject.tp_clear"><code class="xref c c-member docutils literal"><span class="pre">tp_clear</span></code></a>.</p>
728
<p>More information about Python’s garbage collection scheme can be found in
729
section <a class="reference internal" href="gcsupport.html#supporting-cycle-detection"><span>Supporting Cyclic Garbage Collection</span></a>.</p>
730
<p>This field is inherited by subtypes together with <a class="reference internal" href="#c.PyTypeObject.tp_traverse" title="PyTypeObject.tp_traverse"><code class="xref c c-member docutils literal"><span class="pre">tp_traverse</span></code></a> and the
731
<a class="reference internal" href="#Py_TPFLAGS_HAVE_GC" title="Py_TPFLAGS_HAVE_GC"><code class="xref py py-const docutils literal"><span class="pre">Py_TPFLAGS_HAVE_GC</span></code></a> flag bit: the flag bit, <a class="reference internal" href="#c.PyTypeObject.tp_traverse" title="PyTypeObject.tp_traverse"><code class="xref c c-member docutils literal"><span class="pre">tp_traverse</span></code></a>, and
732
<a class="reference internal" href="#c.PyTypeObject.tp_clear" title="PyTypeObject.tp_clear"><code class="xref c c-member docutils literal"><span class="pre">tp_clear</span></code></a> are all inherited from the base type if they are all zero in
737
<dt id="c.PyTypeObject.tp_richcompare">
738
richcmpfunc <code class="descname">PyTypeObject.tp_richcompare</code><a class="headerlink" href="#c.PyTypeObject.tp_richcompare" title="Permalink to this definition">¶</a></dt>
739
<dd><p>An optional pointer to the rich comparison function, whose signature is
740
<code class="docutils literal"><span class="pre">PyObject</span> <span class="pre">*tp_richcompare(PyObject</span> <span class="pre">*a,</span> <span class="pre">PyObject</span> <span class="pre">*b,</span> <span class="pre">int</span> <span class="pre">op)</span></code>. The first
741
parameter is guaranteed to be an instance of the type that is defined
742
by <a class="reference internal" href="type.html#c.PyTypeObject" title="PyTypeObject"><code class="xref c c-type docutils literal"><span class="pre">PyTypeObject</span></code></a>.</p>
743
<p>The function should return the result of the comparison (usually <code class="docutils literal"><span class="pre">Py_True</span></code>
744
or <code class="docutils literal"><span class="pre">Py_False</span></code>). If the comparison is undefined, it must return
745
<code class="docutils literal"><span class="pre">Py_NotImplemented</span></code>, if another error occurred it must return <code class="docutils literal"><span class="pre">NULL</span></code> and
746
set an exception condition.</p>
747
<div class="admonition note">
748
<p class="first admonition-title">Note</p>
749
<p class="last">If you want to implement a type for which only a limited set of
750
comparisons makes sense (e.g. <code class="docutils literal"><span class="pre">==</span></code> and <code class="docutils literal"><span class="pre">!=</span></code>, but not <code class="docutils literal"><span class="pre"><</span></code> and
751
friends), directly raise <a class="reference internal" href="../library/exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal"><span class="pre">TypeError</span></code></a> in the rich comparison function.</p>
753
<p>This field is inherited by subtypes together with <a class="reference internal" href="#c.PyTypeObject.tp_hash" title="PyTypeObject.tp_hash"><code class="xref c c-member docutils literal"><span class="pre">tp_hash</span></code></a>:
754
a subtype inherits <a class="reference internal" href="#c.PyTypeObject.tp_richcompare" title="PyTypeObject.tp_richcompare"><code class="xref c c-member docutils literal"><span class="pre">tp_richcompare</span></code></a> and <a class="reference internal" href="#c.PyTypeObject.tp_hash" title="PyTypeObject.tp_hash"><code class="xref c c-member docutils literal"><span class="pre">tp_hash</span></code></a> when
755
the subtype’s <a class="reference internal" href="#c.PyTypeObject.tp_richcompare" title="PyTypeObject.tp_richcompare"><code class="xref c c-member docutils literal"><span class="pre">tp_richcompare</span></code></a> and <a class="reference internal" href="#c.PyTypeObject.tp_hash" title="PyTypeObject.tp_hash"><code class="xref c c-member docutils literal"><span class="pre">tp_hash</span></code></a> are both
757
<p>The following constants are defined to be used as the third argument for
758
<a class="reference internal" href="#c.PyTypeObject.tp_richcompare" title="PyTypeObject.tp_richcompare"><code class="xref c c-member docutils literal"><span class="pre">tp_richcompare</span></code></a> and for <a class="reference internal" href="object.html#c.PyObject_RichCompare" title="PyObject_RichCompare"><code class="xref c c-func docutils literal"><span class="pre">PyObject_RichCompare()</span></code></a>:</p>
759
<table border="1" class="docutils">
764
<thead valign="bottom">
765
<tr class="row-odd"><th class="head">Constant</th>
766
<th class="head">Comparison</th>
770
<tr class="row-even"><td><code class="xref py py-const docutils literal"><span class="pre">Py_LT</span></code></td>
771
<td><code class="docutils literal"><span class="pre"><</span></code></td>
773
<tr class="row-odd"><td><code class="xref py py-const docutils literal"><span class="pre">Py_LE</span></code></td>
774
<td><code class="docutils literal"><span class="pre"><=</span></code></td>
776
<tr class="row-even"><td><code class="xref py py-const docutils literal"><span class="pre">Py_EQ</span></code></td>
777
<td><code class="docutils literal"><span class="pre">==</span></code></td>
779
<tr class="row-odd"><td><code class="xref py py-const docutils literal"><span class="pre">Py_NE</span></code></td>
780
<td><code class="docutils literal"><span class="pre">!=</span></code></td>
782
<tr class="row-even"><td><code class="xref py py-const docutils literal"><span class="pre">Py_GT</span></code></td>
783
<td><code class="docutils literal"><span class="pre">></span></code></td>
785
<tr class="row-odd"><td><code class="xref py py-const docutils literal"><span class="pre">Py_GE</span></code></td>
786
<td><code class="docutils literal"><span class="pre">>=</span></code></td>
793
<dt id="c.PyTypeObject.tp_weaklistoffset">
794
Py_ssize_t <code class="descname">PyTypeObject.tp_weaklistoffset</code><a class="headerlink" href="#c.PyTypeObject.tp_weaklistoffset" title="Permalink to this definition">¶</a></dt>
795
<dd><p>If the instances of this type are weakly referenceable, this field is greater
796
than zero and contains the offset in the instance structure of the weak
797
reference list head (ignoring the GC header, if present); this offset is used by
798
<code class="xref c c-func docutils literal"><span class="pre">PyObject_ClearWeakRefs()</span></code> and the <code class="xref c c-func docutils literal"><span class="pre">PyWeakref_*()</span></code> functions. The
799
instance structure needs to include a field of type <a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><code class="xref c c-type docutils literal"><span class="pre">PyObject*</span></code></a> which is
800
initialized to <em>NULL</em>.</p>
801
<p>Do not confuse this field with <a class="reference internal" href="#c.PyTypeObject.tp_weaklist" title="PyTypeObject.tp_weaklist"><code class="xref c c-member docutils literal"><span class="pre">tp_weaklist</span></code></a>; that is the list head for
802
weak references to the type object itself.</p>
803
<p>This field is inherited by subtypes, but see the rules listed below. A subtype
804
may override this offset; this means that the subtype uses a different weak
805
reference list head than the base type. Since the list head is always found via
806
<a class="reference internal" href="#c.PyTypeObject.tp_weaklistoffset" title="PyTypeObject.tp_weaklistoffset"><code class="xref c c-member docutils literal"><span class="pre">tp_weaklistoffset</span></code></a>, this should not be a problem.</p>
807
<p>When a type defined by a class statement has no <a class="reference internal" href="../reference/datamodel.html#object.__slots__" title="object.__slots__"><code class="xref py py-attr docutils literal"><span class="pre">__slots__</span></code></a> declaration,
808
and none of its base types are weakly referenceable, the type is made weakly
809
referenceable by adding a weak reference list head slot to the instance layout
810
and setting the <a class="reference internal" href="#c.PyTypeObject.tp_weaklistoffset" title="PyTypeObject.tp_weaklistoffset"><code class="xref c c-member docutils literal"><span class="pre">tp_weaklistoffset</span></code></a> of that slot’s offset.</p>
811
<p>When a type’s <code class="xref py py-attr docutils literal"><span class="pre">__slots__</span></code> declaration contains a slot named
812
<code class="xref py py-attr docutils literal"><span class="pre">__weakref__</span></code>, that slot becomes the weak reference list head for
813
instances of the type, and the slot’s offset is stored in the type’s
814
<a class="reference internal" href="#c.PyTypeObject.tp_weaklistoffset" title="PyTypeObject.tp_weaklistoffset"><code class="xref c c-member docutils literal"><span class="pre">tp_weaklistoffset</span></code></a>.</p>
815
<p>When a type’s <code class="xref py py-attr docutils literal"><span class="pre">__slots__</span></code> declaration does not contain a slot named
816
<code class="xref py py-attr docutils literal"><span class="pre">__weakref__</span></code>, the type inherits its <a class="reference internal" href="#c.PyTypeObject.tp_weaklistoffset" title="PyTypeObject.tp_weaklistoffset"><code class="xref c c-member docutils literal"><span class="pre">tp_weaklistoffset</span></code></a> from its
821
<dt id="c.PyTypeObject.tp_iter">
822
getiterfunc <code class="descname">PyTypeObject.tp_iter</code><a class="headerlink" href="#c.PyTypeObject.tp_iter" title="Permalink to this definition">¶</a></dt>
823
<dd><p>An optional pointer to a function that returns an iterator for the object. Its
824
presence normally signals that the instances of this type are iterable (although
825
sequences may be iterable without this function).</p>
826
<p>This function has the same signature as <a class="reference internal" href="object.html#c.PyObject_GetIter" title="PyObject_GetIter"><code class="xref c c-func docutils literal"><span class="pre">PyObject_GetIter()</span></code></a>.</p>
827
<p>This field is inherited by subtypes.</p>
831
<dt id="c.PyTypeObject.tp_iternext">
832
iternextfunc <code class="descname">PyTypeObject.tp_iternext</code><a class="headerlink" href="#c.PyTypeObject.tp_iternext" title="Permalink to this definition">¶</a></dt>
833
<dd><p>An optional pointer to a function that returns the next item in an iterator.
834
When the iterator is exhausted, it must return <em>NULL</em>; a <a class="reference internal" href="../library/exceptions.html#StopIteration" title="StopIteration"><code class="xref py py-exc docutils literal"><span class="pre">StopIteration</span></code></a>
835
exception may or may not be set. When another error occurs, it must return
836
<em>NULL</em> too. Its presence signals that the instances of this type are
838
<p>Iterator types should also define the <a class="reference internal" href="#c.PyTypeObject.tp_iter" title="PyTypeObject.tp_iter"><code class="xref c c-member docutils literal"><span class="pre">tp_iter</span></code></a> function, and that
839
function should return the iterator instance itself (not a new iterator
841
<p>This function has the same signature as <a class="reference internal" href="iter.html#c.PyIter_Next" title="PyIter_Next"><code class="xref c c-func docutils literal"><span class="pre">PyIter_Next()</span></code></a>.</p>
842
<p>This field is inherited by subtypes.</p>
846
<dt id="c.PyTypeObject.tp_methods">
847
struct <a class="reference internal" href="structures.html#c.PyMethodDef" title="PyMethodDef">PyMethodDef</a>* <code class="descname">PyTypeObject.tp_methods</code><a class="headerlink" href="#c.PyTypeObject.tp_methods" title="Permalink to this definition">¶</a></dt>
848
<dd><p>An optional pointer to a static <em>NULL</em>-terminated array of <a class="reference internal" href="structures.html#c.PyMethodDef" title="PyMethodDef"><code class="xref c c-type docutils literal"><span class="pre">PyMethodDef</span></code></a>
849
structures, declaring regular methods of this type.</p>
850
<p>For each entry in the array, an entry is added to the type’s dictionary (see
851
<a class="reference internal" href="#c.PyTypeObject.tp_dict" title="PyTypeObject.tp_dict"><code class="xref c c-member docutils literal"><span class="pre">tp_dict</span></code></a> below) containing a method descriptor.</p>
852
<p>This field is not inherited by subtypes (methods are inherited through a
853
different mechanism).</p>
857
<dt id="c.PyTypeObject.tp_members">
858
struct <a class="reference internal" href="structures.html#c.PyMemberDef" title="PyMemberDef">PyMemberDef</a>* <code class="descname">PyTypeObject.tp_members</code><a class="headerlink" href="#c.PyTypeObject.tp_members" title="Permalink to this definition">¶</a></dt>
859
<dd><p>An optional pointer to a static <em>NULL</em>-terminated array of <a class="reference internal" href="structures.html#c.PyMemberDef" title="PyMemberDef"><code class="xref c c-type docutils literal"><span class="pre">PyMemberDef</span></code></a>
860
structures, declaring regular data members (fields or slots) of instances of
862
<p>For each entry in the array, an entry is added to the type’s dictionary (see
863
<a class="reference internal" href="#c.PyTypeObject.tp_dict" title="PyTypeObject.tp_dict"><code class="xref c c-member docutils literal"><span class="pre">tp_dict</span></code></a> below) containing a member descriptor.</p>
864
<p>This field is not inherited by subtypes (members are inherited through a
865
different mechanism).</p>
869
<dt id="c.PyTypeObject.tp_getset">
870
struct PyGetSetDef* <code class="descname">PyTypeObject.tp_getset</code><a class="headerlink" href="#c.PyTypeObject.tp_getset" title="Permalink to this definition">¶</a></dt>
871
<dd><p>An optional pointer to a static <em>NULL</em>-terminated array of <code class="xref c c-type docutils literal"><span class="pre">PyGetSetDef</span></code>
872
structures, declaring computed attributes of instances of this type.</p>
873
<p>For each entry in the array, an entry is added to the type’s dictionary (see
874
<a class="reference internal" href="#c.PyTypeObject.tp_dict" title="PyTypeObject.tp_dict"><code class="xref c c-member docutils literal"><span class="pre">tp_dict</span></code></a> below) containing a getset descriptor.</p>
875
<p>This field is not inherited by subtypes (computed attributes are inherited
876
through a different mechanism).</p>
877
<p>Docs for PyGetSetDef:</p>
878
<div class="highlight-c"><div class="highlight"><pre><span class="k">typedef</span> <span class="n">PyObject</span> <span class="o">*</span><span class="p">(</span><span class="o">*</span><span class="n">getter</span><span class="p">)(</span><span class="n">PyObject</span> <span class="o">*</span><span class="p">,</span> <span class="kt">void</span> <span class="o">*</span><span class="p">);</span>
879
<span class="k">typedef</span> <span class="nf">int</span> <span class="p">(</span><span class="o">*</span><span class="n">setter</span><span class="p">)(</span><span class="n">PyObject</span> <span class="o">*</span><span class="p">,</span> <span class="n">PyObject</span> <span class="o">*</span><span class="p">,</span> <span class="kt">void</span> <span class="o">*</span><span class="p">);</span>
881
<span class="k">typedef</span> <span class="k">struct</span> <span class="n">PyGetSetDef</span> <span class="p">{</span>
882
<span class="kt">char</span> <span class="o">*</span><span class="n">name</span><span class="p">;</span> <span class="cm">/* attribute name */</span>
883
<span class="n">getter</span> <span class="n">get</span><span class="p">;</span> <span class="cm">/* C function to get the attribute */</span>
884
<span class="n">setter</span> <span class="n">set</span><span class="p">;</span> <span class="cm">/* C function to set or delete the attribute */</span>
885
<span class="kt">char</span> <span class="o">*</span><span class="n">doc</span><span class="p">;</span> <span class="cm">/* optional doc string */</span>
886
<span class="kt">void</span> <span class="o">*</span><span class="n">closure</span><span class="p">;</span> <span class="cm">/* optional additional data for getter and setter */</span>
887
<span class="p">}</span> <span class="n">PyGetSetDef</span><span class="p">;</span>
893
<dt id="c.PyTypeObject.tp_base">
894
<a class="reference internal" href="type.html#c.PyTypeObject" title="PyTypeObject">PyTypeObject</a>* <code class="descname">PyTypeObject.tp_base</code><a class="headerlink" href="#c.PyTypeObject.tp_base" title="Permalink to this definition">¶</a></dt>
895
<dd><p>An optional pointer to a base type from which type properties are inherited. At
896
this level, only single inheritance is supported; multiple inheritance require
897
dynamically creating a type object by calling the metatype.</p>
898
<p>This field is not inherited by subtypes (obviously), but it defaults to
899
<code class="docutils literal"><span class="pre">&PyBaseObject_Type</span></code> (which to Python programmers is known as the type
900
<a class="reference internal" href="../library/functions.html#object" title="object"><code class="xref py py-class docutils literal"><span class="pre">object</span></code></a>).</p>
904
<dt id="c.PyTypeObject.tp_dict">
905
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyTypeObject.tp_dict</code><a class="headerlink" href="#c.PyTypeObject.tp_dict" title="Permalink to this definition">¶</a></dt>
906
<dd><p>The type’s dictionary is stored here by <a class="reference internal" href="type.html#c.PyType_Ready" title="PyType_Ready"><code class="xref c c-func docutils literal"><span class="pre">PyType_Ready()</span></code></a>.</p>
907
<p>This field should normally be initialized to <em>NULL</em> before PyType_Ready is
908
called; it may also be initialized to a dictionary containing initial attributes
909
for the type. Once <a class="reference internal" href="type.html#c.PyType_Ready" title="PyType_Ready"><code class="xref c c-func docutils literal"><span class="pre">PyType_Ready()</span></code></a> has initialized the type, extra
910
attributes for the type may be added to this dictionary only if they don’t
911
correspond to overloaded operations (like <a class="reference internal" href="../reference/datamodel.html#object.__add__" title="object.__add__"><code class="xref py py-meth docutils literal"><span class="pre">__add__()</span></code></a>).</p>
912
<p>This field is not inherited by subtypes (though the attributes defined in here
913
are inherited through a different mechanism).</p>
914
<div class="admonition warning">
915
<p class="first admonition-title">Warning</p>
916
<p class="last">It is not safe to use <a class="reference internal" href="dict.html#c.PyDict_SetItem" title="PyDict_SetItem"><code class="xref c c-func docutils literal"><span class="pre">PyDict_SetItem()</span></code></a> on or otherwise modify
917
<a class="reference internal" href="#c.PyTypeObject.tp_dict" title="PyTypeObject.tp_dict"><code class="xref c c-member docutils literal"><span class="pre">tp_dict</span></code></a> with the dictionary C-API.</p>
922
<dt id="c.PyTypeObject.tp_descr_get">
923
descrgetfunc <code class="descname">PyTypeObject.tp_descr_get</code><a class="headerlink" href="#c.PyTypeObject.tp_descr_get" title="Permalink to this definition">¶</a></dt>
924
<dd><p>An optional pointer to a “descriptor get” function.</p>
925
<p>The function signature is</p>
926
<div class="highlight-c"><div class="highlight"><pre><span class="n">PyObject</span> <span class="o">*</span> <span class="nf">tp_descr_get</span><span class="p">(</span><span class="n">PyObject</span> <span class="o">*</span><span class="n">self</span><span class="p">,</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">obj</span><span class="p">,</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">type</span><span class="p">);</span>
929
<p>This field is inherited by subtypes.</p>
933
<dt id="c.PyTypeObject.tp_descr_set">
934
descrsetfunc <code class="descname">PyTypeObject.tp_descr_set</code><a class="headerlink" href="#c.PyTypeObject.tp_descr_set" title="Permalink to this definition">¶</a></dt>
935
<dd><p>An optional pointer to a function for setting and deleting
936
a descriptor’s value.</p>
937
<p>The function signature is</p>
938
<div class="highlight-c"><div class="highlight"><pre><span class="kt">int</span> <span class="nf">tp_descr_set</span><span class="p">(</span><span class="n">PyObject</span> <span class="o">*</span><span class="n">self</span><span class="p">,</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">obj</span><span class="p">,</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">value</span><span class="p">);</span>
941
<p>The <em>value</em> argument is set to <em>NULL</em> to delete the value.
942
This field is inherited by subtypes.</p>
946
<dt id="c.PyTypeObject.tp_dictoffset">
947
Py_ssize_t <code class="descname">PyTypeObject.tp_dictoffset</code><a class="headerlink" href="#c.PyTypeObject.tp_dictoffset" title="Permalink to this definition">¶</a></dt>
948
<dd><p>If the instances of this type have a dictionary containing instance variables,
949
this field is non-zero and contains the offset in the instances of the type of
950
the instance variable dictionary; this offset is used by
951
<a class="reference internal" href="object.html#c.PyObject_GenericGetAttr" title="PyObject_GenericGetAttr"><code class="xref c c-func docutils literal"><span class="pre">PyObject_GenericGetAttr()</span></code></a>.</p>
952
<p>Do not confuse this field with <a class="reference internal" href="#c.PyTypeObject.tp_dict" title="PyTypeObject.tp_dict"><code class="xref c c-member docutils literal"><span class="pre">tp_dict</span></code></a>; that is the dictionary for
953
attributes of the type object itself.</p>
954
<p>If the value of this field is greater than zero, it specifies the offset from
955
the start of the instance structure. If the value is less than zero, it
956
specifies the offset from the <em>end</em> of the instance structure. A negative
957
offset is more expensive to use, and should only be used when the instance
958
structure contains a variable-length part. This is used for example to add an
959
instance variable dictionary to subtypes of <a class="reference internal" href="../library/stdtypes.html#str" title="str"><code class="xref py py-class docutils literal"><span class="pre">str</span></code></a> or <a class="reference internal" href="../library/stdtypes.html#tuple" title="tuple"><code class="xref py py-class docutils literal"><span class="pre">tuple</span></code></a>. Note
960
that the <a class="reference internal" href="#c.PyTypeObject.tp_basicsize" title="PyTypeObject.tp_basicsize"><code class="xref c c-member docutils literal"><span class="pre">tp_basicsize</span></code></a> field should account for the dictionary added to
961
the end in that case, even though the dictionary is not included in the basic
962
object layout. On a system with a pointer size of 4 bytes,
963
<a class="reference internal" href="#c.PyTypeObject.tp_dictoffset" title="PyTypeObject.tp_dictoffset"><code class="xref c c-member docutils literal"><span class="pre">tp_dictoffset</span></code></a> should be set to <code class="docutils literal"><span class="pre">-4</span></code> to indicate that the dictionary is
964
at the very end of the structure.</p>
965
<p>The real dictionary offset in an instance can be computed from a negative
966
<a class="reference internal" href="#c.PyTypeObject.tp_dictoffset" title="PyTypeObject.tp_dictoffset"><code class="xref c c-member docutils literal"><span class="pre">tp_dictoffset</span></code></a> as follows:</p>
967
<div class="highlight-c"><div class="highlight"><pre><span class="n">dictoffset</span> <span class="o">=</span> <span class="n">tp_basicsize</span> <span class="o">+</span> <span class="n">abs</span><span class="p">(</span><span class="n">ob_size</span><span class="p">)</span><span class="o">*</span><span class="n">tp_itemsize</span> <span class="o">+</span> <span class="n">tp_dictoffset</span>
968
<span class="k">if</span> <span class="n">dictoffset</span> <span class="n">is</span> <span class="n">not</span> <span class="n">aligned</span> <span class="n">on</span> <span class="k">sizeof</span><span class="p">(</span><span class="kt">void</span><span class="o">*</span><span class="p">)</span><span class="o">:</span>
969
<span class="n">round</span> <span class="n">up</span> <span class="n">to</span> <span class="k">sizeof</span><span class="p">(</span><span class="kt">void</span><span class="o">*</span><span class="p">)</span>
972
<p>where <a class="reference internal" href="#c.PyTypeObject.tp_basicsize" title="PyTypeObject.tp_basicsize"><code class="xref c c-member docutils literal"><span class="pre">tp_basicsize</span></code></a>, <a class="reference internal" href="#c.PyTypeObject.tp_itemsize" title="PyTypeObject.tp_itemsize"><code class="xref c c-member docutils literal"><span class="pre">tp_itemsize</span></code></a> and <a class="reference internal" href="#c.PyTypeObject.tp_dictoffset" title="PyTypeObject.tp_dictoffset"><code class="xref c c-member docutils literal"><span class="pre">tp_dictoffset</span></code></a> are
973
taken from the type object, and <code class="xref py py-attr docutils literal"><span class="pre">ob_size</span></code> is taken from the instance. The
974
absolute value is taken because ints use the sign of <code class="xref py py-attr docutils literal"><span class="pre">ob_size</span></code> to
975
store the sign of the number. (There’s never a need to do this calculation
976
yourself; it is done for you by <code class="xref c c-func docutils literal"><span class="pre">_PyObject_GetDictPtr()</span></code>.)</p>
977
<p>This field is inherited by subtypes, but see the rules listed below. A subtype
978
may override this offset; this means that the subtype instances store the
979
dictionary at a difference offset than the base type. Since the dictionary is
980
always found via <a class="reference internal" href="#c.PyTypeObject.tp_dictoffset" title="PyTypeObject.tp_dictoffset"><code class="xref c c-member docutils literal"><span class="pre">tp_dictoffset</span></code></a>, this should not be a problem.</p>
981
<p>When a type defined by a class statement has no <a class="reference internal" href="../reference/datamodel.html#object.__slots__" title="object.__slots__"><code class="xref py py-attr docutils literal"><span class="pre">__slots__</span></code></a> declaration,
982
and none of its base types has an instance variable dictionary, a dictionary
983
slot is added to the instance layout and the <a class="reference internal" href="#c.PyTypeObject.tp_dictoffset" title="PyTypeObject.tp_dictoffset"><code class="xref c c-member docutils literal"><span class="pre">tp_dictoffset</span></code></a> is set to
984
that slot’s offset.</p>
985
<p>When a type defined by a class statement has a <code class="xref py py-attr docutils literal"><span class="pre">__slots__</span></code> declaration,
986
the type inherits its <a class="reference internal" href="#c.PyTypeObject.tp_dictoffset" title="PyTypeObject.tp_dictoffset"><code class="xref c c-member docutils literal"><span class="pre">tp_dictoffset</span></code></a> from its base type.</p>
987
<p>(Adding a slot named <a class="reference internal" href="../library/stdtypes.html#object.__dict__" title="object.__dict__"><code class="xref py py-attr docutils literal"><span class="pre">__dict__</span></code></a> to the <code class="xref py py-attr docutils literal"><span class="pre">__slots__</span></code> declaration does
988
not have the expected effect, it just causes confusion. Maybe this should be
989
added as a feature just like <code class="xref py py-attr docutils literal"><span class="pre">__weakref__</span></code> though.)</p>
993
<dt id="c.PyTypeObject.tp_init">
994
initproc <code class="descname">PyTypeObject.tp_init</code><a class="headerlink" href="#c.PyTypeObject.tp_init" title="Permalink to this definition">¶</a></dt>
995
<dd><p>An optional pointer to an instance initialization function.</p>
996
<p>This function corresponds to the <a class="reference internal" href="../reference/datamodel.html#object.__init__" title="object.__init__"><code class="xref py py-meth docutils literal"><span class="pre">__init__()</span></code></a> method of classes. Like
997
<a class="reference internal" href="../reference/datamodel.html#object.__init__" title="object.__init__"><code class="xref py py-meth docutils literal"><span class="pre">__init__()</span></code></a>, it is possible to create an instance without calling
998
<a class="reference internal" href="../reference/datamodel.html#object.__init__" title="object.__init__"><code class="xref py py-meth docutils literal"><span class="pre">__init__()</span></code></a>, and it is possible to reinitialize an instance by calling its
999
<a class="reference internal" href="../reference/datamodel.html#object.__init__" title="object.__init__"><code class="xref py py-meth docutils literal"><span class="pre">__init__()</span></code></a> method again.</p>
1000
<p>The function signature is</p>
1001
<div class="highlight-c"><div class="highlight"><pre><span class="kt">int</span> <span class="n">tp_init</span><span class="p">(</span><span class="n">PyObject</span> <span class="o">*</span><span class="n">self</span><span class="p">,</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">kwds</span><span class="p">)</span>
1004
<p>The self argument is the instance to be initialized; the <em>args</em> and <em>kwds</em>
1005
arguments represent positional and keyword arguments of the call to
1006
<a class="reference internal" href="../reference/datamodel.html#object.__init__" title="object.__init__"><code class="xref py py-meth docutils literal"><span class="pre">__init__()</span></code></a>.</p>
1007
<p>The <a class="reference internal" href="#c.PyTypeObject.tp_init" title="PyTypeObject.tp_init"><code class="xref c c-member docutils literal"><span class="pre">tp_init</span></code></a> function, if not <em>NULL</em>, is called when an instance is
1008
created normally by calling its type, after the type’s <a class="reference internal" href="#c.PyTypeObject.tp_new" title="PyTypeObject.tp_new"><code class="xref c c-member docutils literal"><span class="pre">tp_new</span></code></a> function
1009
has returned an instance of the type. If the <a class="reference internal" href="#c.PyTypeObject.tp_new" title="PyTypeObject.tp_new"><code class="xref c c-member docutils literal"><span class="pre">tp_new</span></code></a> function returns an
1010
instance of some other type that is not a subtype of the original type, no
1011
<a class="reference internal" href="#c.PyTypeObject.tp_init" title="PyTypeObject.tp_init"><code class="xref c c-member docutils literal"><span class="pre">tp_init</span></code></a> function is called; if <a class="reference internal" href="#c.PyTypeObject.tp_new" title="PyTypeObject.tp_new"><code class="xref c c-member docutils literal"><span class="pre">tp_new</span></code></a> returns an instance of a
1012
subtype of the original type, the subtype’s <a class="reference internal" href="#c.PyTypeObject.tp_init" title="PyTypeObject.tp_init"><code class="xref c c-member docutils literal"><span class="pre">tp_init</span></code></a> is called.</p>
1013
<p>This field is inherited by subtypes.</p>
1017
<dt id="c.PyTypeObject.tp_alloc">
1018
allocfunc <code class="descname">PyTypeObject.tp_alloc</code><a class="headerlink" href="#c.PyTypeObject.tp_alloc" title="Permalink to this definition">¶</a></dt>
1019
<dd><p>An optional pointer to an instance allocation function.</p>
1020
<p>The function signature is</p>
1021
<div class="highlight-c"><div class="highlight"><pre><span class="n">PyObject</span> <span class="o">*</span><span class="n">tp_alloc</span><span class="p">(</span><span class="n">PyTypeObject</span> <span class="o">*</span><span class="n">self</span><span class="p">,</span> <span class="n">Py_ssize_t</span> <span class="n">nitems</span><span class="p">)</span>
1024
<p>The purpose of this function is to separate memory allocation from memory
1025
initialization. It should return a pointer to a block of memory of adequate
1026
length for the instance, suitably aligned, and initialized to zeros, but with
1027
<code class="xref py py-attr docutils literal"><span class="pre">ob_refcnt</span></code> set to <code class="docutils literal"><span class="pre">1</span></code> and <code class="xref py py-attr docutils literal"><span class="pre">ob_type</span></code> set to the type argument. If
1028
the type’s <a class="reference internal" href="#c.PyTypeObject.tp_itemsize" title="PyTypeObject.tp_itemsize"><code class="xref c c-member docutils literal"><span class="pre">tp_itemsize</span></code></a> is non-zero, the object’s <code class="xref py py-attr docutils literal"><span class="pre">ob_size</span></code> field
1029
should be initialized to <em>nitems</em> and the length of the allocated memory block
1030
should be <code class="docutils literal"><span class="pre">tp_basicsize</span> <span class="pre">+</span> <span class="pre">nitems*tp_itemsize</span></code>, rounded up to a multiple of
1031
<code class="docutils literal"><span class="pre">sizeof(void*)</span></code>; otherwise, <em>nitems</em> is not used and the length of the block
1032
should be <a class="reference internal" href="#c.PyTypeObject.tp_basicsize" title="PyTypeObject.tp_basicsize"><code class="xref c c-member docutils literal"><span class="pre">tp_basicsize</span></code></a>.</p>
1033
<p>Do not use this function to do any other instance initialization, not even to
1034
allocate additional memory; that should be done by <a class="reference internal" href="#c.PyTypeObject.tp_new" title="PyTypeObject.tp_new"><code class="xref c c-member docutils literal"><span class="pre">tp_new</span></code></a>.</p>
1035
<p>This field is inherited by static subtypes, but not by dynamic subtypes
1036
(subtypes created by a class statement); in the latter, this field is always set
1037
to <a class="reference internal" href="type.html#c.PyType_GenericAlloc" title="PyType_GenericAlloc"><code class="xref c c-func docutils literal"><span class="pre">PyType_GenericAlloc()</span></code></a>, to force a standard heap allocation strategy.
1038
That is also the recommended value for statically defined types.</p>
1042
<dt id="c.PyTypeObject.tp_new">
1043
newfunc <code class="descname">PyTypeObject.tp_new</code><a class="headerlink" href="#c.PyTypeObject.tp_new" title="Permalink to this definition">¶</a></dt>
1044
<dd><p>An optional pointer to an instance creation function.</p>
1045
<p>If this function is <em>NULL</em> for a particular type, that type cannot be called to
1046
create new instances; presumably there is some other way to create instances,
1047
like a factory function.</p>
1048
<p>The function signature is</p>
1049
<div class="highlight-c"><div class="highlight"><pre><span class="n">PyObject</span> <span class="o">*</span><span class="n">tp_new</span><span class="p">(</span><span class="n">PyTypeObject</span> <span class="o">*</span><span class="n">subtype</span><span class="p">,</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">kwds</span><span class="p">)</span>
1052
<p>The subtype argument is the type of the object being created; the <em>args</em> and
1053
<em>kwds</em> arguments represent positional and keyword arguments of the call to the
1054
type. Note that subtype doesn’t have to equal the type whose <a class="reference internal" href="#c.PyTypeObject.tp_new" title="PyTypeObject.tp_new"><code class="xref c c-member docutils literal"><span class="pre">tp_new</span></code></a>
1055
function is called; it may be a subtype of that type (but not an unrelated
1057
<p>The <a class="reference internal" href="#c.PyTypeObject.tp_new" title="PyTypeObject.tp_new"><code class="xref c c-member docutils literal"><span class="pre">tp_new</span></code></a> function should call <code class="docutils literal"><span class="pre">subtype->tp_alloc(subtype,</span> <span class="pre">nitems)</span></code>
1058
to allocate space for the object, and then do only as much further
1059
initialization as is absolutely necessary. Initialization that can safely be
1060
ignored or repeated should be placed in the <a class="reference internal" href="#c.PyTypeObject.tp_init" title="PyTypeObject.tp_init"><code class="xref c c-member docutils literal"><span class="pre">tp_init</span></code></a> handler. A good
1061
rule of thumb is that for immutable types, all initialization should take place
1062
in <a class="reference internal" href="#c.PyTypeObject.tp_new" title="PyTypeObject.tp_new"><code class="xref c c-member docutils literal"><span class="pre">tp_new</span></code></a>, while for mutable types, most initialization should be
1063
deferred to <a class="reference internal" href="#c.PyTypeObject.tp_init" title="PyTypeObject.tp_init"><code class="xref c c-member docutils literal"><span class="pre">tp_init</span></code></a>.</p>
1064
<p>This field is inherited by subtypes, except it is not inherited by static types
1065
whose <a class="reference internal" href="#c.PyTypeObject.tp_base" title="PyTypeObject.tp_base"><code class="xref c c-member docutils literal"><span class="pre">tp_base</span></code></a> is <em>NULL</em> or <code class="docutils literal"><span class="pre">&PyBaseObject_Type</span></code>.</p>
1069
<dt id="c.PyTypeObject.tp_free">
1070
destructor <code class="descname">PyTypeObject.tp_free</code><a class="headerlink" href="#c.PyTypeObject.tp_free" title="Permalink to this definition">¶</a></dt>
1071
<dd><p>An optional pointer to an instance deallocation function. Its signature is
1072
<code class="xref c c-type docutils literal"><span class="pre">freefunc</span></code>:</p>
1073
<div class="highlight-c"><div class="highlight"><pre><span class="kt">void</span> <span class="n">tp_free</span><span class="p">(</span><span class="kt">void</span> <span class="o">*</span><span class="p">)</span>
1076
<p>An initializer that is compatible with this signature is <code class="xref c c-func docutils literal"><span class="pre">PyObject_Free()</span></code>.</p>
1077
<p>This field is inherited by static subtypes, but not by dynamic subtypes
1078
(subtypes created by a class statement); in the latter, this field is set to a
1079
deallocator suitable to match <a class="reference internal" href="type.html#c.PyType_GenericAlloc" title="PyType_GenericAlloc"><code class="xref c c-func docutils literal"><span class="pre">PyType_GenericAlloc()</span></code></a> and the value of the
1080
<a class="reference internal" href="#Py_TPFLAGS_HAVE_GC" title="Py_TPFLAGS_HAVE_GC"><code class="xref py py-const docutils literal"><span class="pre">Py_TPFLAGS_HAVE_GC</span></code></a> flag bit.</p>
1084
<dt id="c.PyTypeObject.tp_is_gc">
1085
<a class="reference internal" href="gcsupport.html#c.inquiry" title="inquiry">inquiry</a> <code class="descname">PyTypeObject.tp_is_gc</code><a class="headerlink" href="#c.PyTypeObject.tp_is_gc" title="Permalink to this definition">¶</a></dt>
1086
<dd><p>An optional pointer to a function called by the garbage collector.</p>
1087
<p>The garbage collector needs to know whether a particular object is collectible
1088
or not. Normally, it is sufficient to look at the object’s type’s
1089
<a class="reference internal" href="#c.PyTypeObject.tp_flags" title="PyTypeObject.tp_flags"><code class="xref c c-member docutils literal"><span class="pre">tp_flags</span></code></a> field, and check the <a class="reference internal" href="#Py_TPFLAGS_HAVE_GC" title="Py_TPFLAGS_HAVE_GC"><code class="xref py py-const docutils literal"><span class="pre">Py_TPFLAGS_HAVE_GC</span></code></a> flag bit. But
1090
some types have a mixture of statically and dynamically allocated instances, and
1091
the statically allocated instances are not collectible. Such types should
1092
define this function; it should return <code class="docutils literal"><span class="pre">1</span></code> for a collectible instance, and
1093
<code class="docutils literal"><span class="pre">0</span></code> for a non-collectible instance. The signature is</p>
1094
<div class="highlight-c"><div class="highlight"><pre><span class="kt">int</span> <span class="n">tp_is_gc</span><span class="p">(</span><span class="n">PyObject</span> <span class="o">*</span><span class="n">self</span><span class="p">)</span>
1097
<p>(The only example of this are types themselves. The metatype,
1098
<a class="reference internal" href="type.html#c.PyType_Type" title="PyType_Type"><code class="xref c c-data docutils literal"><span class="pre">PyType_Type</span></code></a>, defines this function to distinguish between statically
1099
and dynamically allocated types.)</p>
1100
<p>This field is inherited by subtypes.</p>
1104
<dt id="c.PyTypeObject.tp_bases">
1105
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyTypeObject.tp_bases</code><a class="headerlink" href="#c.PyTypeObject.tp_bases" title="Permalink to this definition">¶</a></dt>
1106
<dd><p>Tuple of base types.</p>
1107
<p>This is set for types created by a class statement. It should be <em>NULL</em> for
1108
statically defined types.</p>
1109
<p>This field is not inherited.</p>
1113
<dt id="c.PyTypeObject.tp_mro">
1114
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyTypeObject.tp_mro</code><a class="headerlink" href="#c.PyTypeObject.tp_mro" title="Permalink to this definition">¶</a></dt>
1115
<dd><p>Tuple containing the expanded set of base types, starting with the type itself
1116
and ending with <a class="reference internal" href="../library/functions.html#object" title="object"><code class="xref py py-class docutils literal"><span class="pre">object</span></code></a>, in Method Resolution Order.</p>
1117
<p>This field is not inherited; it is calculated fresh by <a class="reference internal" href="type.html#c.PyType_Ready" title="PyType_Ready"><code class="xref c c-func docutils literal"><span class="pre">PyType_Ready()</span></code></a>.</p>
1121
<dt id="c.PyTypeObject.tp_finalize">
1122
destructor <code class="descname">PyTypeObject.tp_finalize</code><a class="headerlink" href="#c.PyTypeObject.tp_finalize" title="Permalink to this definition">¶</a></dt>
1123
<dd><p>An optional pointer to an instance finalization function. Its signature is
1124
<code class="xref c c-type docutils literal"><span class="pre">destructor</span></code>:</p>
1125
<div class="highlight-c"><div class="highlight"><pre><span class="kt">void</span> <span class="n">tp_finalize</span><span class="p">(</span><span class="n">PyObject</span> <span class="o">*</span><span class="p">)</span>
1128
<p>If <a class="reference internal" href="#c.PyTypeObject.tp_finalize" title="PyTypeObject.tp_finalize"><code class="xref c c-member docutils literal"><span class="pre">tp_finalize</span></code></a> is set, the interpreter calls it once when
1129
finalizing an instance. It is called either from the garbage
1130
collector (if the instance is part of an isolated reference cycle) or
1131
just before the object is deallocated. Either way, it is guaranteed
1132
to be called before attempting to break reference cycles, ensuring
1133
that it finds the object in a sane state.</p>
1134
<p><a class="reference internal" href="#c.PyTypeObject.tp_finalize" title="PyTypeObject.tp_finalize"><code class="xref c c-member docutils literal"><span class="pre">tp_finalize</span></code></a> should not mutate the current exception status;
1135
therefore, a recommended way to write a non-trivial finalizer is:</p>
1136
<div class="highlight-c"><div class="highlight"><pre><span class="k">static</span> <span class="kt">void</span>
1137
<span class="nf">local_finalize</span><span class="p">(</span><span class="n">PyObject</span> <span class="o">*</span><span class="n">self</span><span class="p">)</span>
1138
<span class="p">{</span>
1139
<span class="n">PyObject</span> <span class="o">*</span><span class="n">error_type</span><span class="p">,</span> <span class="o">*</span><span class="n">error_value</span><span class="p">,</span> <span class="o">*</span><span class="n">error_traceback</span><span class="p">;</span>
1141
<span class="cm">/* Save the current exception, if any. */</span>
1142
<span class="n">PyErr_Fetch</span><span class="p">(</span><span class="o">&</span><span class="n">error_type</span><span class="p">,</span> <span class="o">&</span><span class="n">error_value</span><span class="p">,</span> <span class="o">&</span><span class="n">error_traceback</span><span class="p">);</span>
1144
<span class="cm">/* ... */</span>
1146
<span class="cm">/* Restore the saved exception. */</span>
1147
<span class="n">PyErr_Restore</span><span class="p">(</span><span class="n">error_type</span><span class="p">,</span> <span class="n">error_value</span><span class="p">,</span> <span class="n">error_traceback</span><span class="p">);</span>
1148
<span class="p">}</span>
1151
<p>For this field to be taken into account (even through inheritance),
1152
you must also set the <a class="reference internal" href="#Py_TPFLAGS_HAVE_FINALIZE" title="Py_TPFLAGS_HAVE_FINALIZE"><code class="xref py py-const docutils literal"><span class="pre">Py_TPFLAGS_HAVE_FINALIZE</span></code></a> flags bit.</p>
1153
<p>This field is inherited by subtypes.</p>
1154
<div class="versionadded">
1155
<p><span class="versionmodified">New in version 3.4.</span></p>
1157
<div class="admonition seealso">
1158
<p class="first admonition-title">See also</p>
1159
<p class="last">“Safe object finalization” (<span class="target" id="index-3"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0442"><strong>PEP 442</strong></a>)</p>
1164
<dt id="c.PyTypeObject.tp_cache">
1165
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyTypeObject.tp_cache</code><a class="headerlink" href="#c.PyTypeObject.tp_cache" title="Permalink to this definition">¶</a></dt>
1166
<dd><p>Unused. Not inherited. Internal use only.</p>
1170
<dt id="c.PyTypeObject.tp_subclasses">
1171
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyTypeObject.tp_subclasses</code><a class="headerlink" href="#c.PyTypeObject.tp_subclasses" title="Permalink to this definition">¶</a></dt>
1172
<dd><p>List of weak references to subclasses. Not inherited. Internal use only.</p>
1176
<dt id="c.PyTypeObject.tp_weaklist">
1177
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyTypeObject.tp_weaklist</code><a class="headerlink" href="#c.PyTypeObject.tp_weaklist" title="Permalink to this definition">¶</a></dt>
1178
<dd><p>Weak reference list head, for weak references to this type object. Not
1179
inherited. Internal use only.</p>
1182
<p>The remaining fields are only defined if the feature test macro
1183
<code class="xref py py-const docutils literal"><span class="pre">COUNT_ALLOCS</span></code> is defined, and are for internal use only. They are
1184
documented here for completeness. None of these fields are inherited by
1187
<dt id="c.PyTypeObject.tp_allocs">
1188
Py_ssize_t <code class="descname">PyTypeObject.tp_allocs</code><a class="headerlink" href="#c.PyTypeObject.tp_allocs" title="Permalink to this definition">¶</a></dt>
1189
<dd><p>Number of allocations.</p>
1193
<dt id="c.PyTypeObject.tp_frees">
1194
Py_ssize_t <code class="descname">PyTypeObject.tp_frees</code><a class="headerlink" href="#c.PyTypeObject.tp_frees" title="Permalink to this definition">¶</a></dt>
1195
<dd><p>Number of frees.</p>
1199
<dt id="c.PyTypeObject.tp_maxalloc">
1200
Py_ssize_t <code class="descname">PyTypeObject.tp_maxalloc</code><a class="headerlink" href="#c.PyTypeObject.tp_maxalloc" title="Permalink to this definition">¶</a></dt>
1201
<dd><p>Maximum simultaneously allocated objects.</p>
1205
<dt id="c.PyTypeObject.tp_next">
1206
<a class="reference internal" href="type.html#c.PyTypeObject" title="PyTypeObject">PyTypeObject</a>* <code class="descname">PyTypeObject.tp_next</code><a class="headerlink" href="#c.PyTypeObject.tp_next" title="Permalink to this definition">¶</a></dt>
1207
<dd><p>Pointer to the next type object with a non-zero <a class="reference internal" href="#c.PyTypeObject.tp_allocs" title="PyTypeObject.tp_allocs"><code class="xref c c-member docutils literal"><span class="pre">tp_allocs</span></code></a> field.</p>
1210
<p>Also, note that, in a garbage collected Python, tp_dealloc may be called from
1211
any Python thread, not just the thread which created the object (if the object
1212
becomes part of a refcount cycle, that cycle might be collected by a garbage
1213
collection on any thread). This is not a problem for Python API calls, since
1214
the thread on which tp_dealloc is called will own the Global Interpreter Lock
1215
(GIL). However, if the object being destroyed in turn destroys objects from some
1216
other C or C++ library, care should be taken to ensure that destroying those
1217
objects on the thread which called tp_dealloc will not violate any assumptions
1220
<div class="section" id="number-object-structures">
1221
<span id="number-structs"></span><h1>Number Object Structures<a class="headerlink" href="#number-object-structures" title="Permalink to this headline">¶</a></h1>
1223
<dt id="c.PyNumberMethods">
1224
<code class="descname">PyNumberMethods</code><a class="headerlink" href="#c.PyNumberMethods" title="Permalink to this definition">¶</a></dt>
1225
<dd><p>This structure holds pointers to the functions which an object uses to
1226
implement the number protocol. Each function is used by the function of
1227
similar name documented in the <a class="reference internal" href="number.html#number"><span>Number Protocol</span></a> section.</p>
1228
<p>Here is the structure definition:</p>
1229
<div class="highlight-c"><div class="highlight"><pre><span class="k">typedef</span> <span class="k">struct</span> <span class="p">{</span>
1230
<span class="n">binaryfunc</span> <span class="n">nb_add</span><span class="p">;</span>
1231
<span class="n">binaryfunc</span> <span class="n">nb_subtract</span><span class="p">;</span>
1232
<span class="n">binaryfunc</span> <span class="n">nb_multiply</span><span class="p">;</span>
1233
<span class="n">binaryfunc</span> <span class="n">nb_remainder</span><span class="p">;</span>
1234
<span class="n">binaryfunc</span> <span class="n">nb_divmod</span><span class="p">;</span>
1235
<span class="n">ternaryfunc</span> <span class="n">nb_power</span><span class="p">;</span>
1236
<span class="n">unaryfunc</span> <span class="n">nb_negative</span><span class="p">;</span>
1237
<span class="n">unaryfunc</span> <span class="n">nb_positive</span><span class="p">;</span>
1238
<span class="n">unaryfunc</span> <span class="n">nb_absolute</span><span class="p">;</span>
1239
<span class="n">inquiry</span> <span class="n">nb_bool</span><span class="p">;</span>
1240
<span class="n">unaryfunc</span> <span class="n">nb_invert</span><span class="p">;</span>
1241
<span class="n">binaryfunc</span> <span class="n">nb_lshift</span><span class="p">;</span>
1242
<span class="n">binaryfunc</span> <span class="n">nb_rshift</span><span class="p">;</span>
1243
<span class="n">binaryfunc</span> <span class="n">nb_and</span><span class="p">;</span>
1244
<span class="n">binaryfunc</span> <span class="n">nb_xor</span><span class="p">;</span>
1245
<span class="n">binaryfunc</span> <span class="n">nb_or</span><span class="p">;</span>
1246
<span class="n">unaryfunc</span> <span class="n">nb_int</span><span class="p">;</span>
1247
<span class="kt">void</span> <span class="o">*</span><span class="n">nb_reserved</span><span class="p">;</span>
1248
<span class="n">unaryfunc</span> <span class="n">nb_float</span><span class="p">;</span>
1250
<span class="n">binaryfunc</span> <span class="n">nb_inplace_add</span><span class="p">;</span>
1251
<span class="n">binaryfunc</span> <span class="n">nb_inplace_subtract</span><span class="p">;</span>
1252
<span class="n">binaryfunc</span> <span class="n">nb_inplace_multiply</span><span class="p">;</span>
1253
<span class="n">binaryfunc</span> <span class="n">nb_inplace_remainder</span><span class="p">;</span>
1254
<span class="n">ternaryfunc</span> <span class="n">nb_inplace_power</span><span class="p">;</span>
1255
<span class="n">binaryfunc</span> <span class="n">nb_inplace_lshift</span><span class="p">;</span>
1256
<span class="n">binaryfunc</span> <span class="n">nb_inplace_rshift</span><span class="p">;</span>
1257
<span class="n">binaryfunc</span> <span class="n">nb_inplace_and</span><span class="p">;</span>
1258
<span class="n">binaryfunc</span> <span class="n">nb_inplace_xor</span><span class="p">;</span>
1259
<span class="n">binaryfunc</span> <span class="n">nb_inplace_or</span><span class="p">;</span>
1261
<span class="n">binaryfunc</span> <span class="n">nb_floor_divide</span><span class="p">;</span>
1262
<span class="n">binaryfunc</span> <span class="n">nb_true_divide</span><span class="p">;</span>
1263
<span class="n">binaryfunc</span> <span class="n">nb_inplace_floor_divide</span><span class="p">;</span>
1264
<span class="n">binaryfunc</span> <span class="n">nb_inplace_true_divide</span><span class="p">;</span>
1266
<span class="n">unaryfunc</span> <span class="n">nb_index</span><span class="p">;</span>
1268
<span class="n">binaryfunc</span> <span class="n">nb_matrix_multiply</span><span class="p">;</span>
1269
<span class="n">binaryfunc</span> <span class="n">nb_inplace_matrix_multiply</span><span class="p">;</span>
1270
<span class="p">}</span> <span class="n">PyNumberMethods</span><span class="p">;</span>
1273
<div class="admonition note">
1274
<p class="first admonition-title">Note</p>
1275
<p class="last">Binary and ternary functions must check the type of all their operands,
1276
and implement the necessary conversions (at least one of the operands is
1277
an instance of the defined type). If the operation is not defined for the
1278
given operands, binary and ternary functions must return
1279
<code class="docutils literal"><span class="pre">Py_NotImplemented</span></code>, if another error occurred they must return <code class="docutils literal"><span class="pre">NULL</span></code>
1280
and set an exception.</p>
1282
<div class="admonition note">
1283
<p class="first admonition-title">Note</p>
1284
<p class="last">The <code class="xref c c-data docutils literal"><span class="pre">nb_reserved</span></code> field should always be <code class="docutils literal"><span class="pre">NULL</span></code>. It
1285
was previously called <code class="xref c c-data docutils literal"><span class="pre">nb_long</span></code>, and was renamed in
1291
<div class="section" id="mapping-object-structures">
1292
<span id="mapping-structs"></span><h1>Mapping Object Structures<a class="headerlink" href="#mapping-object-structures" title="Permalink to this headline">¶</a></h1>
1294
<dt id="c.PyMappingMethods">
1295
<code class="descname">PyMappingMethods</code><a class="headerlink" href="#c.PyMappingMethods" title="Permalink to this definition">¶</a></dt>
1296
<dd><p>This structure holds pointers to the functions which an object uses to
1297
implement the mapping protocol. It has three members:</p>
1301
<dt id="c.PyMappingMethods.mp_length">
1302
lenfunc <code class="descname">PyMappingMethods.mp_length</code><a class="headerlink" href="#c.PyMappingMethods.mp_length" title="Permalink to this definition">¶</a></dt>
1303
<dd><p>This function is used by <a class="reference internal" href="mapping.html#c.PyMapping_Length" title="PyMapping_Length"><code class="xref c c-func docutils literal"><span class="pre">PyMapping_Length()</span></code></a> and
1304
<a class="reference internal" href="object.html#c.PyObject_Size" title="PyObject_Size"><code class="xref c c-func docutils literal"><span class="pre">PyObject_Size()</span></code></a>, and has the same signature. This slot may be set to
1305
<em>NULL</em> if the object has no defined length.</p>
1309
<dt id="c.PyMappingMethods.mp_subscript">
1310
binaryfunc <code class="descname">PyMappingMethods.mp_subscript</code><a class="headerlink" href="#c.PyMappingMethods.mp_subscript" title="Permalink to this definition">¶</a></dt>
1311
<dd><p>This function is used by <a class="reference internal" href="object.html#c.PyObject_GetItem" title="PyObject_GetItem"><code class="xref c c-func docutils literal"><span class="pre">PyObject_GetItem()</span></code></a> and has the same
1312
signature. This slot must be filled for the <a class="reference internal" href="mapping.html#c.PyMapping_Check" title="PyMapping_Check"><code class="xref c c-func docutils literal"><span class="pre">PyMapping_Check()</span></code></a>
1313
function to return <code class="docutils literal"><span class="pre">1</span></code>, it can be <em>NULL</em> otherwise.</p>
1317
<dt id="c.PyMappingMethods.mp_ass_subscript">
1318
objobjargproc <code class="descname">PyMappingMethods.mp_ass_subscript</code><a class="headerlink" href="#c.PyMappingMethods.mp_ass_subscript" title="Permalink to this definition">¶</a></dt>
1319
<dd><p>This function is used by <a class="reference internal" href="object.html#c.PyObject_SetItem" title="PyObject_SetItem"><code class="xref c c-func docutils literal"><span class="pre">PyObject_SetItem()</span></code></a> and
1320
<a class="reference internal" href="object.html#c.PyObject_DelItem" title="PyObject_DelItem"><code class="xref c c-func docutils literal"><span class="pre">PyObject_DelItem()</span></code></a>. It has the same signature as
1321
<a class="reference internal" href="object.html#c.PyObject_SetItem" title="PyObject_SetItem"><code class="xref c c-func docutils literal"><span class="pre">PyObject_SetItem()</span></code></a>, but <em>v</em> can also be set to <em>NULL</em> to delete
1322
an item. If this slot is <em>NULL</em>, the object does not support item
1323
assignment and deletion.</p>
1327
<div class="section" id="sequence-object-structures">
1328
<span id="sequence-structs"></span><h1>Sequence Object Structures<a class="headerlink" href="#sequence-object-structures" title="Permalink to this headline">¶</a></h1>
1330
<dt id="c.PySequenceMethods">
1331
<code class="descname">PySequenceMethods</code><a class="headerlink" href="#c.PySequenceMethods" title="Permalink to this definition">¶</a></dt>
1332
<dd><p>This structure holds pointers to the functions which an object uses to
1333
implement the sequence protocol.</p>
1337
<dt id="c.PySequenceMethods.sq_length">
1338
lenfunc <code class="descname">PySequenceMethods.sq_length</code><a class="headerlink" href="#c.PySequenceMethods.sq_length" title="Permalink to this definition">¶</a></dt>
1339
<dd><p>This function is used by <a class="reference internal" href="sequence.html#c.PySequence_Size" title="PySequence_Size"><code class="xref c c-func docutils literal"><span class="pre">PySequence_Size()</span></code></a> and <a class="reference internal" href="object.html#c.PyObject_Size" title="PyObject_Size"><code class="xref c c-func docutils literal"><span class="pre">PyObject_Size()</span></code></a>,
1340
and has the same signature.</p>
1344
<dt id="c.PySequenceMethods.sq_concat">
1345
binaryfunc <code class="descname">PySequenceMethods.sq_concat</code><a class="headerlink" href="#c.PySequenceMethods.sq_concat" title="Permalink to this definition">¶</a></dt>
1346
<dd><p>This function is used by <a class="reference internal" href="sequence.html#c.PySequence_Concat" title="PySequence_Concat"><code class="xref c c-func docutils literal"><span class="pre">PySequence_Concat()</span></code></a> and has the same
1347
signature. It is also used by the <code class="docutils literal"><span class="pre">+</span></code> operator, after trying the numeric
1348
addition via the <code class="xref c c-member docutils literal"><span class="pre">nb_add</span></code> slot.</p>
1352
<dt id="c.PySequenceMethods.sq_repeat">
1353
ssizeargfunc <code class="descname">PySequenceMethods.sq_repeat</code><a class="headerlink" href="#c.PySequenceMethods.sq_repeat" title="Permalink to this definition">¶</a></dt>
1354
<dd><p>This function is used by <a class="reference internal" href="sequence.html#c.PySequence_Repeat" title="PySequence_Repeat"><code class="xref c c-func docutils literal"><span class="pre">PySequence_Repeat()</span></code></a> and has the same
1355
signature. It is also used by the <code class="docutils literal"><span class="pre">*</span></code> operator, after trying numeric
1356
multiplication via the <code class="xref c c-member docutils literal"><span class="pre">nb_multiply</span></code>
1361
<dt id="c.PySequenceMethods.sq_item">
1362
ssizeargfunc <code class="descname">PySequenceMethods.sq_item</code><a class="headerlink" href="#c.PySequenceMethods.sq_item" title="Permalink to this definition">¶</a></dt>
1363
<dd><p>This function is used by <a class="reference internal" href="sequence.html#c.PySequence_GetItem" title="PySequence_GetItem"><code class="xref c c-func docutils literal"><span class="pre">PySequence_GetItem()</span></code></a> and has the same
1364
signature. This slot must be filled for the <a class="reference internal" href="sequence.html#c.PySequence_Check" title="PySequence_Check"><code class="xref c c-func docutils literal"><span class="pre">PySequence_Check()</span></code></a>
1365
function to return <code class="docutils literal"><span class="pre">1</span></code>, it can be <em>NULL</em> otherwise.</p>
1366
<p>Negative indexes are handled as follows: if the <code class="xref py py-attr docutils literal"><span class="pre">sq_length</span></code> slot is
1367
filled, it is called and the sequence length is used to compute a positive
1368
index which is passed to <code class="xref py py-attr docutils literal"><span class="pre">sq_item</span></code>. If <code class="xref py py-attr docutils literal"><span class="pre">sq_length</span></code> is <em>NULL</em>,
1369
the index is passed as is to the function.</p>
1373
<dt id="c.PySequenceMethods.sq_ass_item">
1374
ssizeobjargproc <code class="descname">PySequenceMethods.sq_ass_item</code><a class="headerlink" href="#c.PySequenceMethods.sq_ass_item" title="Permalink to this definition">¶</a></dt>
1375
<dd><p>This function is used by <a class="reference internal" href="sequence.html#c.PySequence_SetItem" title="PySequence_SetItem"><code class="xref c c-func docutils literal"><span class="pre">PySequence_SetItem()</span></code></a> and has the same
1376
signature. This slot may be left to <em>NULL</em> if the object does not support
1377
item assignment and deletion.</p>
1381
<dt id="c.PySequenceMethods.sq_contains">
1382
objobjproc <code class="descname">PySequenceMethods.sq_contains</code><a class="headerlink" href="#c.PySequenceMethods.sq_contains" title="Permalink to this definition">¶</a></dt>
1383
<dd><p>This function may be used by <a class="reference internal" href="sequence.html#c.PySequence_Contains" title="PySequence_Contains"><code class="xref c c-func docutils literal"><span class="pre">PySequence_Contains()</span></code></a> and has the same
1384
signature. This slot may be left to <em>NULL</em>, in this case
1385
<a class="reference internal" href="sequence.html#c.PySequence_Contains" title="PySequence_Contains"><code class="xref c c-func docutils literal"><span class="pre">PySequence_Contains()</span></code></a> simply traverses the sequence until it finds a
1390
<dt id="c.PySequenceMethods.sq_inplace_concat">
1391
binaryfunc <code class="descname">PySequenceMethods.sq_inplace_concat</code><a class="headerlink" href="#c.PySequenceMethods.sq_inplace_concat" title="Permalink to this definition">¶</a></dt>
1392
<dd><p>This function is used by <a class="reference internal" href="sequence.html#c.PySequence_InPlaceConcat" title="PySequence_InPlaceConcat"><code class="xref c c-func docutils literal"><span class="pre">PySequence_InPlaceConcat()</span></code></a> and has the same
1393
signature. It should modify its first operand, and return it.</p>
1397
<dt id="c.PySequenceMethods.sq_inplace_repeat">
1398
ssizeargfunc <code class="descname">PySequenceMethods.sq_inplace_repeat</code><a class="headerlink" href="#c.PySequenceMethods.sq_inplace_repeat" title="Permalink to this definition">¶</a></dt>
1399
<dd><p>This function is used by <a class="reference internal" href="sequence.html#c.PySequence_InPlaceRepeat" title="PySequence_InPlaceRepeat"><code class="xref c c-func docutils literal"><span class="pre">PySequence_InPlaceRepeat()</span></code></a> and has the same
1400
signature. It should modify its first operand, and return it.</p>
1404
<div class="section" id="buffer-object-structures">
1405
<span id="buffer-structs"></span><h1>Buffer Object Structures<a class="headerlink" href="#buffer-object-structures" title="Permalink to this headline">¶</a></h1>
1407
<dt id="c.PyBufferProcs">
1408
<code class="descname">PyBufferProcs</code><a class="headerlink" href="#c.PyBufferProcs" title="Permalink to this definition">¶</a></dt>
1409
<dd><p>This structure holds pointers to the functions required by the
1410
<a class="reference internal" href="buffer.html#bufferobjects"><span>Buffer protocol</span></a>. The protocol defines how
1411
an exporter object can expose its internal data to consumer objects.</p>
1415
<dt id="c.PyBufferProcs.bf_getbuffer">
1416
getbufferproc <code class="descname">PyBufferProcs.bf_getbuffer</code><a class="headerlink" href="#c.PyBufferProcs.bf_getbuffer" title="Permalink to this definition">¶</a></dt>
1417
<dd><p>The signature of this function is:</p>
1418
<div class="highlight-c"><div class="highlight"><pre><span class="kt">int</span> <span class="p">(</span><span class="n">PyObject</span> <span class="o">*</span><span class="n">exporter</span><span class="p">,</span> <span class="n">Py_buffer</span> <span class="o">*</span><span class="n">view</span><span class="p">,</span> <span class="kt">int</span> <span class="n">flags</span><span class="p">);</span>
1421
<p>Handle a request to <em>exporter</em> to fill in <em>view</em> as specified by <em>flags</em>.
1422
Except for point (3), an implementation of this function MUST take these
1424
<ol class="arabic simple">
1425
<li>Check if the request can be met. If not, raise <code class="xref c c-data docutils literal"><span class="pre">PyExc_BufferError</span></code>,
1426
set <code class="xref c c-data docutils literal"><span class="pre">view->obj</span></code> to <em>NULL</em> and return -1.</li>
1427
<li>Fill in the requested fields.</li>
1428
<li>Increment an internal counter for the number of exports.</li>
1429
<li>Set <code class="xref c c-data docutils literal"><span class="pre">view->obj</span></code> to <em>exporter</em> and increment <code class="xref c c-data docutils literal"><span class="pre">view->obj</span></code>.</li>
1432
<p>If <em>exporter</em> is part of a chain or tree of buffer providers, two main
1433
schemes can be used:</p>
1435
<li>Re-export: Each member of the tree acts as the exporting object and
1436
sets <code class="xref c c-data docutils literal"><span class="pre">view->obj</span></code> to a new reference to itself.</li>
1437
<li>Redirect: The buffer request is redirected to the root object of the
1438
tree. Here, <code class="xref c c-data docutils literal"><span class="pre">view->obj</span></code> will be a new reference to the root
1441
<p>The individual fields of <em>view</em> are described in section
1442
<a class="reference internal" href="buffer.html#buffer-structure"><span>Buffer structure</span></a>, the rules how an exporter
1443
must react to specific requests are in section
1444
<a class="reference internal" href="buffer.html#buffer-request-types"><span>Buffer request types</span></a>.</p>
1445
<p>All memory pointed to in the <a class="reference internal" href="buffer.html#c.Py_buffer" title="Py_buffer"><code class="xref c c-type docutils literal"><span class="pre">Py_buffer</span></code></a> structure belongs to
1446
the exporter and must remain valid until there are no consumers left.
1447
<a class="reference internal" href="buffer.html#c.Py_buffer.format" title="Py_buffer.format"><code class="xref c c-member docutils literal"><span class="pre">format</span></code></a>, <a class="reference internal" href="buffer.html#c.Py_buffer.shape" title="Py_buffer.shape"><code class="xref c c-member docutils literal"><span class="pre">shape</span></code></a>,
1448
<a class="reference internal" href="buffer.html#c.Py_buffer.strides" title="Py_buffer.strides"><code class="xref c c-member docutils literal"><span class="pre">strides</span></code></a>, <a class="reference internal" href="buffer.html#c.Py_buffer.suboffsets" title="Py_buffer.suboffsets"><code class="xref c c-member docutils literal"><span class="pre">suboffsets</span></code></a>
1449
and <a class="reference internal" href="buffer.html#c.Py_buffer.internal" title="Py_buffer.internal"><code class="xref c c-member docutils literal"><span class="pre">internal</span></code></a>
1450
are read-only for the consumer.</p>
1451
<p><a class="reference internal" href="buffer.html#c.PyBuffer_FillInfo" title="PyBuffer_FillInfo"><code class="xref c c-func docutils literal"><span class="pre">PyBuffer_FillInfo()</span></code></a> provides an easy way of exposing a simple
1452
bytes buffer while dealing correctly with all request types.</p>
1453
<p><a class="reference internal" href="buffer.html#c.PyObject_GetBuffer" title="PyObject_GetBuffer"><code class="xref c c-func docutils literal"><span class="pre">PyObject_GetBuffer()</span></code></a> is the interface for the consumer that
1454
wraps this function.</p>
1458
<dt id="c.PyBufferProcs.bf_releasebuffer">
1459
releasebufferproc <code class="descname">PyBufferProcs.bf_releasebuffer</code><a class="headerlink" href="#c.PyBufferProcs.bf_releasebuffer" title="Permalink to this definition">¶</a></dt>
1460
<dd><p>The signature of this function is:</p>
1461
<div class="highlight-c"><div class="highlight"><pre><span class="kt">void</span> <span class="p">(</span><span class="n">PyObject</span> <span class="o">*</span><span class="n">exporter</span><span class="p">,</span> <span class="n">Py_buffer</span> <span class="o">*</span><span class="n">view</span><span class="p">);</span>
1464
<p>Handle a request to release the resources of the buffer. If no resources
1465
need to be released, <a class="reference internal" href="#c.PyBufferProcs.bf_releasebuffer" title="PyBufferProcs.bf_releasebuffer"><code class="xref c c-member docutils literal"><span class="pre">PyBufferProcs.bf_releasebuffer</span></code></a> may be
1466
<em>NULL</em>. Otherwise, a standard implementation of this function will take
1467
these optional steps:</p>
1468
<ol class="arabic simple">
1469
<li>Decrement an internal counter for the number of exports.</li>
1470
<li>If the counter is 0, free all memory associated with <em>view</em>.</li>
1472
<p>The exporter MUST use the <a class="reference internal" href="buffer.html#c.Py_buffer.internal" title="Py_buffer.internal"><code class="xref c c-member docutils literal"><span class="pre">internal</span></code></a> field to keep
1473
track of buffer-specific resources. This field is guaranteed to remain
1474
constant, while a consumer MAY pass a copy of the original buffer as the
1475
<em>view</em> argument.</p>
1476
<p>This function MUST NOT decrement <code class="xref c c-data docutils literal"><span class="pre">view->obj</span></code>, since that is
1477
done automatically in <a class="reference internal" href="buffer.html#c.PyBuffer_Release" title="PyBuffer_Release"><code class="xref c c-func docutils literal"><span class="pre">PyBuffer_Release()</span></code></a> (this scheme is
1478
useful for breaking reference cycles).</p>
1479
<p><a class="reference internal" href="buffer.html#c.PyBuffer_Release" title="PyBuffer_Release"><code class="xref c c-func docutils literal"><span class="pre">PyBuffer_Release()</span></code></a> is the interface for the consumer that
1480
wraps this function.</p>
1484
<div class="section" id="async-object-structures">
1485
<span id="async-structs"></span><h1>Async Object Structures<a class="headerlink" href="#async-object-structures" title="Permalink to this headline">¶</a></h1>
1486
<div class="versionadded">
1487
<p><span class="versionmodified">New in version 3.5.</span></p>
1490
<dt id="c.PyAsyncMethods">
1491
<code class="descname">PyAsyncMethods</code><a class="headerlink" href="#c.PyAsyncMethods" title="Permalink to this definition">¶</a></dt>
1492
<dd><p>This structure holds pointers to the functions required to implement
1493
<a class="reference internal" href="../glossary.html#term-awaitable"><span class="xref std std-term">awaitable</span></a> and <a class="reference internal" href="../glossary.html#term-asynchronous-iterator"><span class="xref std std-term">asynchronous iterator</span></a> objects.</p>
1494
<p>Here is the structure definition:</p>
1495
<div class="highlight-c"><div class="highlight"><pre><span class="k">typedef</span> <span class="k">struct</span> <span class="p">{</span>
1496
<span class="n">unaryfunc</span> <span class="n">am_await</span><span class="p">;</span>
1497
<span class="n">unaryfunc</span> <span class="n">am_aiter</span><span class="p">;</span>
1498
<span class="n">unaryfunc</span> <span class="n">am_anext</span><span class="p">;</span>
1499
<span class="p">}</span> <span class="n">PyAsyncMethods</span><span class="p">;</span>
1505
<dt id="c.PyAsyncMethods.am_await">
1506
unaryfunc <code class="descname">PyAsyncMethods.am_await</code><a class="headerlink" href="#c.PyAsyncMethods.am_await" title="Permalink to this definition">¶</a></dt>
1507
<dd><p>The signature of this function is:</p>
1508
<div class="highlight-c"><div class="highlight"><pre><span class="n">PyObject</span> <span class="o">*</span><span class="n">am_await</span><span class="p">(</span><span class="n">PyObject</span> <span class="o">*</span><span class="n">self</span><span class="p">)</span>
1511
<p>The returned object must be an iterator, i.e. <a class="reference internal" href="iter.html#c.PyIter_Check" title="PyIter_Check"><code class="xref c c-func docutils literal"><span class="pre">PyIter_Check()</span></code></a> must
1512
return <code class="docutils literal"><span class="pre">1</span></code> for it.</p>
1513
<p>This slot may be set to <em>NULL</em> if an object is not an <a class="reference internal" href="../glossary.html#term-awaitable"><span class="xref std std-term">awaitable</span></a>.</p>
1517
<dt id="c.PyAsyncMethods.am_aiter">
1518
unaryfunc <code class="descname">PyAsyncMethods.am_aiter</code><a class="headerlink" href="#c.PyAsyncMethods.am_aiter" title="Permalink to this definition">¶</a></dt>
1519
<dd><p>The signature of this function is:</p>
1520
<div class="highlight-c"><div class="highlight"><pre><span class="n">PyObject</span> <span class="o">*</span><span class="n">am_aiter</span><span class="p">(</span><span class="n">PyObject</span> <span class="o">*</span><span class="n">self</span><span class="p">)</span>
1523
<p>Must return an <a class="reference internal" href="../glossary.html#term-awaitable"><span class="xref std std-term">awaitable</span></a> object. See <a class="reference internal" href="../reference/datamodel.html#object.__anext__" title="object.__anext__"><code class="xref py py-meth docutils literal"><span class="pre">__anext__()</span></code></a> for details.</p>
1524
<p>This slot may be set to <em>NULL</em> if an object does not implement
1525
asynchronous iteration protocol.</p>
1529
<dt id="c.PyAsyncMethods.am_anext">
1530
unaryfunc <code class="descname">PyAsyncMethods.am_anext</code><a class="headerlink" href="#c.PyAsyncMethods.am_anext" title="Permalink to this definition">¶</a></dt>
1531
<dd><p>The signature of this function is:</p>
1532
<div class="highlight-c"><div class="highlight"><pre><span class="n">PyObject</span> <span class="o">*</span><span class="n">am_anext</span><span class="p">(</span><span class="n">PyObject</span> <span class="o">*</span><span class="n">self</span><span class="p">)</span>
1535
<p>Must return an <a class="reference internal" href="../glossary.html#term-awaitable"><span class="xref std std-term">awaitable</span></a> object. See <a class="reference internal" href="../reference/datamodel.html#object.__anext__" title="object.__anext__"><code class="xref py py-meth docutils literal"><span class="pre">__anext__()</span></code></a> for details.
1536
This slot may be set to <em>NULL</em>.</p>
1545
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
1546
<div class="sphinxsidebarwrapper">
1547
<h3><a href="../contents.html">Table Of Contents</a></h3>
1549
<li><a class="reference internal" href="#">Type Objects</a></li>
1550
<li><a class="reference internal" href="#number-object-structures">Number Object Structures</a></li>
1551
<li><a class="reference internal" href="#mapping-object-structures">Mapping Object Structures</a></li>
1552
<li><a class="reference internal" href="#sequence-object-structures">Sequence Object Structures</a></li>
1553
<li><a class="reference internal" href="#buffer-object-structures">Buffer Object Structures</a></li>
1554
<li><a class="reference internal" href="#async-object-structures">Async Object Structures</a></li>
1557
<h4>Previous topic</h4>
1558
<p class="topless"><a href="structures.html"
1559
title="previous chapter">Common Object Structures</a></p>
1561
<p class="topless"><a href="gcsupport.html"
1562
title="next chapter">Supporting Cyclic Garbage Collection</a></p>
1564
<ul class="this-page-menu">
1565
<li><a href="../bugs.html">Report a Bug</a></li>
1566
<li><a href="../_sources/c-api/typeobj.txt"
1567
rel="nofollow">Show Source</a></li>
1570
<div id="searchbox" style="display: none" role="search">
1571
<h3>Quick search</h3>
1572
<form class="search" action="../search.html" method="get">
1573
<input type="text" name="q" />
1574
<input type="submit" value="Go" />
1575
<input type="hidden" name="check_keywords" value="yes" />
1576
<input type="hidden" name="area" value="default" />
1578
<p class="searchtip" style="font-size: 90%">
1579
Enter search terms or a module, class or function name.
1582
<script type="text/javascript">$('#searchbox').show(0);</script>
1585
<div class="clearer"></div>
1587
<div class="related" role="navigation" aria-label="related navigation">
1590
<li class="right" style="margin-right: 10px">
1591
<a href="../genindex.html" title="General Index"
1594
<a href="../py-modindex.html" title="Python Module Index"
1597
<a href="gcsupport.html" title="Supporting Cyclic Garbage Collection"
1600
<a href="structures.html" title="Common Object Structures"
1601
>previous</a> |</li>
1602
<li><img src="../_static/py.png" alt=""
1603
style="vertical-align: middle; margin-top: -1px"/></li>
1604
<li><a href="https://www.python.org/">Python</a> »</li>
1606
<span class="version_switcher_placeholder">3.5.1</span>
1607
<a href="../index.html">Documentation </a> »
1610
<li class="nav-item nav-item-1"><a href="index.html" >Python/C API Reference Manual</a> »</li>
1611
<li class="nav-item nav-item-2"><a href="objimpl.html" >Object Implementation Support</a> »</li>
1614
<div class="footer">
1615
© <a href="../copyright.html">Copyright</a> 1990-2016, Python Software Foundation.
1617
The Python Software Foundation is a non-profit corporation.
1618
<a href="https://www.python.org/psf/donations/">Please donate.</a>
1620
Last updated on Jan 22, 2016.
1621
<a href="../bugs.html">Found a bug</a>?
1623
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.3.3.
b'\\ No newline at end of file'