~dkuhlman/python-training-materials/Materials

« back to all changes in this revision

Viewing changes to python-3.5.1-docs-html/c-api/typeobj.html

  • Committer: Dave Kuhlman
  • Date: 2017-04-15 16:24:56 UTC
  • Revision ID: dkuhlman@davekuhlman.org-20170415162456-iav9vozzg4iwqwv3
Updated docs

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2
 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
 
 
4
 
 
5
 
<html xmlns="http://www.w3.org/1999/xhtml">
6
 
  <head>
7
 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
8
 
    
9
 
    <title>Type Objects &mdash; Python 3.5.1 documentation</title>
10
 
    
11
 
    <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
12
 
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
13
 
    
14
 
    <script type="text/javascript">
15
 
      var DOCUMENTATION_OPTIONS = {
16
 
        URL_ROOT:    '../',
17
 
        VERSION:     '3.5.1',
18
 
        COLLAPSE_INDEX: false,
19
 
        FILE_SUFFIX: '.html',
20
 
        HAS_SOURCE:  true
21
 
      };
22
 
    </script>
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>
39
 
    
40
 
 
41
 
 
42
 
  </head>
43
 
  <body role="document">  
44
 
    <div class="related" role="navigation" aria-label="related navigation">
45
 
      <h3>Navigation</h3>
46
 
      <ul>
47
 
        <li class="right" style="margin-right: 10px">
48
 
          <a href="../genindex.html" title="General Index"
49
 
             accesskey="I">index</a></li>
50
 
        <li class="right" >
51
 
          <a href="../py-modindex.html" title="Python Module Index"
52
 
             >modules</a> |</li>
53
 
        <li class="right" >
54
 
          <a href="gcsupport.html" title="Supporting Cyclic Garbage Collection"
55
 
             accesskey="N">next</a> |</li>
56
 
        <li class="right" >
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> &raquo;</li>
62
 
        <li>
63
 
          <span class="version_switcher_placeholder">3.5.1</span>
64
 
          <a href="../index.html">Documentation </a> &raquo;
65
 
        </li>
66
 
 
67
 
          <li class="nav-item nav-item-1"><a href="index.html" >Python/C API Reference Manual</a> &raquo;</li>
68
 
          <li class="nav-item nav-item-2"><a href="objimpl.html" accesskey="U">Object Implementation Support</a> &raquo;</li> 
69
 
      </ul>
70
 
    </div>    
71
 
 
72
 
    <div class="document">
73
 
      <div class="documentwrapper">
74
 
        <div class="bodywrapper">
75
 
          <div class="body" role="main">
76
 
            
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&#8217;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&#8217;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
91
 
structure.</p>
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 &quot;&lt;module&gt;.&lt;name&gt;&quot; */</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>
103
 
 
104
 
    <span class="cm">/* Methods to implement standard operations */</span>
105
 
 
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>
113
 
 
114
 
    <span class="cm">/* Method suites for standard classes */</span>
115
 
 
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>
119
 
 
120
 
    <span class="cm">/* More standard operations (here for binary compatibility) */</span>
121
 
 
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>
127
 
 
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>
130
 
 
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>
133
 
 
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>
135
 
 
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>
138
 
 
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>
141
 
 
142
 
    <span class="cm">/* rich comparisons */</span>
143
 
    <span class="n">richcmpfunc</span> <span class="n">tp_richcompare</span><span class="p">;</span>
144
 
 
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>
147
 
 
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>
151
 
 
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>
172
 
 
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>
175
 
 
176
 
    <span class="n">destructor</span> <span class="n">tp_finalize</span><span class="p">;</span>
177
 
 
178
 
<span class="p">}</span> <span class="n">PyTypeObject</span><span class="p">;</span>
179
 
</pre></div>
180
 
</div>
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>
186
 
<dl class="member">
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>
200
 
</dd></dl>
201
 
 
202
 
<dl class="member">
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&#8217;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&#8217;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>
211
 
</dd></dl>
212
 
 
213
 
<dl class="member">
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&#8217;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">&amp;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&#8217;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">&amp;</span><span class="n">PyType_Type</span><span class="p">;</span>
225
 
</pre></div>
226
 
</div>
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>
232
 
</dd></dl>
233
 
 
234
 
<dl class="member">
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>
240
 
</dd></dl>
241
 
 
242
 
<dl class="member">
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">&quot;P.Q.M.T&quot;</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>
264
 
</dd></dl>
265
 
 
266
 
<dl class="member">
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 &#8220;length&#8221; of the object.  The value of
279
 
N is typically stored in the instance&#8217;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&#8217;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>
285
 
field).</p>
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&#8217;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>
303
 
</dd></dl>
304
 
 
305
 
<dl class="member">
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&#8217;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&#8217;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>
326
 
</dd></dl>
327
 
 
328
 
<dl class="member">
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>
332
 
</dd></dl>
333
 
 
334
 
<dl class="member">
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&#8217;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>
345
 
</dd></dl>
346
 
 
347
 
<dl class="member">
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&#8217;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>
359
 
</dd></dl>
360
 
 
361
 
<dl class="member">
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>
369
 
</div>
370
 
</dd></dl>
371
 
 
372
 
<dl class="member">
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">'&lt;'</span></code> and ending with <code class="docutils literal"><span class="pre">'&gt;'</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">&lt;%s</span> <span class="pre">object</span> <span class="pre">at</span> <span class="pre">%p&gt;</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&#8217;s
385
 
memory address.</p>
386
 
<p>This field is inherited by subtypes.</p>
387
 
</dd></dl>
388
 
 
389
 
<dl class="member">
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>
397
 
</dd></dl>
398
 
 
399
 
<dl class="member">
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>
407
 
</dd></dl>
408
 
 
409
 
<dl class="member">
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>
417
 
</dd></dl>
418
 
 
419
 
<dl class="member">
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&#8217;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>
441
 
</dd></dl>
442
 
 
443
 
<dl class="member">
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>
450
 
</dd></dl>
451
 
 
452
 
<dl class="member">
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 &#8220;friendly&#8221; 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
464
 
representation.</p>
465
 
<p>This field is inherited by subtypes.</p>
466
 
</dd></dl>
467
 
 
468
 
<dl class="member">
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&#8217;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>
478
 
</dd></dl>
479
 
 
480
 
<dl class="member">
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&#8217;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>
491
 
</dd></dl>
492
 
 
493
 
<dl class="member">
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>
501
 
</dd></dl>
502
 
 
503
 
<dl class="member">
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&#8217;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-&gt;tp_flags</span> <span class="pre">&amp;</span> <span class="pre">f</span></code> is non-zero.</p>
527
 
<dl class="data">
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&#8217;ed when a new instance is created, and
533
 
DECREF&#8217;ed when an instance is destroyed (this does not apply to instances of
534
 
subtypes; only the type referenced by the instance&#8217;s ob_type gets INCREF&#8217;ed or
535
 
DECREF&#8217;ed).</p>
536
 
</dd></dl>
537
 
 
538
 
<dl class="data">
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 &#8220;final&#8221; class in
543
 
Java).</p>
544
 
</dd></dl>
545
 
 
546
 
<dl class="data">
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>
551
 
</dd></dl>
552
 
 
553
 
<dl class="data">
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
557
 
the type object.</p>
558
 
</dd></dl>
559
 
 
560
 
<dl class="data">
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
568
 
the type object.</p>
569
 
</dd></dl>
570
 
 
571
 
<dl class="data">
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>
578
 
</dd></dl>
579
 
 
580
 
<dl class="data">
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>
583
 
<dd></dd></dl>
584
 
 
585
 
<dl class="data">
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>
588
 
<dd></dd></dl>
589
 
 
590
 
<dl class="data">
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>
593
 
<dd></dd></dl>
594
 
 
595
 
<dl class="data">
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>
598
 
<dd></dd></dl>
599
 
 
600
 
<dl class="data">
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>
603
 
<dd></dd></dl>
604
 
 
605
 
<dl class="data">
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>
608
 
<dd></dd></dl>
609
 
 
610
 
<dl class="data">
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>
613
 
<dd></dd></dl>
614
 
 
615
 
<dl class="data">
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>
625
 
</dd></dl>
626
 
 
627
 
<dl class="data">
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
631
 
type structure.</p>
632
 
<div class="versionadded">
633
 
<p><span class="versionmodified">New in version 3.4.</span></p>
634
 
</div>
635
 
</dd></dl>
636
 
 
637
 
</dd></dl>
638
 
 
639
 
<dl class="member">
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>
646
 
</dd></dl>
647
 
 
648
 
<dl class="member">
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&#8217;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&#8217;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">-&gt;</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">-&gt;</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">-&gt;</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>
668
 
</pre></div>
669
 
</div>
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-&gt;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&#8217;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&#8217;t name them just
678
 
anything.</p>
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
682
 
the subtype.</p>
683
 
</dd></dl>
684
 
 
685
 
<dl class="member">
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&#8217;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&#8217;t immediately obvious, and there&#8217;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&#8217;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">-&gt;</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">-&gt;</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">-&gt;</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">-&gt;</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>
711
 
</pre></div>
712
 
</div>
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&#8217;s possible for such code to reference <em>self</em> again,
720
 
it&#8217;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&#8217;s not necessary to clear contained objects like Python strings or Python
725
 
integers, which can&#8217;t participate in reference cycles. On the other hand, it may
726
 
be convenient to clear all contained Python objects, and write the type&#8217;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&#8217;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
733
 
the subtype.</p>
734
 
</dd></dl>
735
 
 
736
 
<dl class="member">
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">&lt;</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>
752
 
</div>
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&#8217;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
756
 
<em>NULL</em>.</p>
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">
760
 
<colgroup>
761
 
<col width="57%" />
762
 
<col width="43%" />
763
 
</colgroup>
764
 
<thead valign="bottom">
765
 
<tr class="row-odd"><th class="head">Constant</th>
766
 
<th class="head">Comparison</th>
767
 
</tr>
768
 
</thead>
769
 
<tbody valign="top">
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">&lt;</span></code></td>
772
 
</tr>
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">&lt;=</span></code></td>
775
 
</tr>
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>
778
 
</tr>
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>
781
 
</tr>
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">&gt;</span></code></td>
784
 
</tr>
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">&gt;=</span></code></td>
787
 
</tr>
788
 
</tbody>
789
 
</table>
790
 
</dd></dl>
791
 
 
792
 
<dl class="member">
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&#8217;s offset.</p>
811
 
<p>When a type&#8217;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&#8217;s offset is stored in the type&#8217;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&#8217;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
817
 
base type.</p>
818
 
</dd></dl>
819
 
 
820
 
<dl class="member">
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>
828
 
</dd></dl>
829
 
 
830
 
<dl class="member">
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
837
 
iterators.</p>
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
840
 
instance).</p>
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>
843
 
</dd></dl>
844
 
 
845
 
<dl class="member">
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&#8217;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>
854
 
</dd></dl>
855
 
 
856
 
<dl class="member">
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
861
 
this type.</p>
862
 
<p>For each entry in the array, an entry is added to the type&#8217;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>
866
 
</dd></dl>
867
 
 
868
 
<dl class="member">
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&#8217;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>
880
 
 
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>
888
 
</pre></div>
889
 
</div>
890
 
</dd></dl>
891
 
 
892
 
<dl class="member">
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">&amp;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>
901
 
</dd></dl>
902
 
 
903
 
<dl class="member">
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&#8217;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&#8217;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>
918
 
</div>
919
 
</dd></dl>
920
 
 
921
 
<dl class="member">
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 &#8220;descriptor get&#8221; 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>
927
 
</pre></div>
928
 
</div>
929
 
<p>This field is inherited by subtypes.</p>
930
 
</dd></dl>
931
 
 
932
 
<dl class="member">
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&#8217;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>
939
 
</pre></div>
940
 
</div>
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>
943
 
</dd></dl>
944
 
 
945
 
<dl class="member">
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>
970
 
</pre></div>
971
 
</div>
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&#8217;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&#8217;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>
990
 
</dd></dl>
991
 
 
992
 
<dl class="member">
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>
1002
 
</pre></div>
1003
 
</div>
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&#8217;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&#8217;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>
1014
 
</dd></dl>
1015
 
 
1016
 
<dl class="member">
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>
1022
 
</pre></div>
1023
 
</div>
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&#8217;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&#8217;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>
1039
 
</dd></dl>
1040
 
 
1041
 
<dl class="member">
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>
1050
 
</pre></div>
1051
 
</div>
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&#8217;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
1056
 
type).</p>
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-&gt;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">&amp;PyBaseObject_Type</span></code>.</p>
1066
 
</dd></dl>
1067
 
 
1068
 
<dl class="member">
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>
1074
 
</pre></div>
1075
 
</div>
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>
1081
 
</dd></dl>
1082
 
 
1083
 
<dl class="member">
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&#8217;s type&#8217;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>
1095
 
</pre></div>
1096
 
</div>
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>
1101
 
</dd></dl>
1102
 
 
1103
 
<dl class="member">
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>
1110
 
</dd></dl>
1111
 
 
1112
 
<dl class="member">
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>
1118
 
</dd></dl>
1119
 
 
1120
 
<dl class="member">
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>
1126
 
</pre></div>
1127
 
</div>
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>
1140
 
 
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">&amp;</span><span class="n">error_type</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">error_value</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">error_traceback</span><span class="p">);</span>
1143
 
 
1144
 
    <span class="cm">/* ... */</span>
1145
 
 
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>
1149
 
</pre></div>
1150
 
</div>
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>
1156
 
</div>
1157
 
<div class="admonition seealso">
1158
 
<p class="first admonition-title">See also</p>
1159
 
<p class="last">&#8220;Safe object finalization&#8221; (<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>
1160
 
</div>
1161
 
</dd></dl>
1162
 
 
1163
 
<dl class="member">
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>
1167
 
</dd></dl>
1168
 
 
1169
 
<dl class="member">
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>
1173
 
</dd></dl>
1174
 
 
1175
 
<dl class="member">
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>
1180
 
</dd></dl>
1181
 
 
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
1185
 
subtypes.</p>
1186
 
<dl class="member">
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>
1190
 
</dd></dl>
1191
 
 
1192
 
<dl class="member">
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>
1196
 
</dd></dl>
1197
 
 
1198
 
<dl class="member">
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>
1202
 
</dd></dl>
1203
 
 
1204
 
<dl class="member">
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>
1208
 
</dd></dl>
1209
 
 
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
1218
 
of the library.</p>
1219
 
</div>
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>
1222
 
<dl class="type">
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>
1249
 
 
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>
1260
 
 
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>
1265
 
 
1266
 
     <span class="n">unaryfunc</span> <span class="n">nb_index</span><span class="p">;</span>
1267
 
 
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>
1271
 
</pre></div>
1272
 
</div>
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>
1281
 
</div>
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
1286
 
Python 3.0.1.</p>
1287
 
</div>
1288
 
</dd></dl>
1289
 
 
1290
 
</div>
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>
1293
 
<dl class="type">
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>
1298
 
</dd></dl>
1299
 
 
1300
 
<dl class="member">
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>
1306
 
</dd></dl>
1307
 
 
1308
 
<dl class="member">
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>
1314
 
</dd></dl>
1315
 
 
1316
 
<dl class="member">
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>
1324
 
</dd></dl>
1325
 
 
1326
 
</div>
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>
1329
 
<dl class="type">
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>
1334
 
</dd></dl>
1335
 
 
1336
 
<dl class="member">
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>
1341
 
</dd></dl>
1342
 
 
1343
 
<dl class="member">
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>
1349
 
</dd></dl>
1350
 
 
1351
 
<dl class="member">
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>
1357
 
slot.</p>
1358
 
</dd></dl>
1359
 
 
1360
 
<dl class="member">
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>
1370
 
</dd></dl>
1371
 
 
1372
 
<dl class="member">
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>
1378
 
</dd></dl>
1379
 
 
1380
 
<dl class="member">
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
1386
 
match.</p>
1387
 
</dd></dl>
1388
 
 
1389
 
<dl class="member">
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>
1394
 
</dd></dl>
1395
 
 
1396
 
<dl class="member">
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>
1401
 
</dd></dl>
1402
 
 
1403
 
</div>
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>
1406
 
<dl class="type">
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>
1412
 
</dd></dl>
1413
 
 
1414
 
<dl class="member">
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>
1419
 
</pre></div>
1420
 
</div>
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
1423
 
steps:</p>
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-&gt;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-&gt;obj</span></code> to <em>exporter</em> and increment <code class="xref c c-data docutils literal"><span class="pre">view-&gt;obj</span></code>.</li>
1430
 
<li>Return 0.</li>
1431
 
</ol>
1432
 
<p>If <em>exporter</em> is part of a chain or tree of buffer providers, two main
1433
 
schemes can be used:</p>
1434
 
<ul class="simple">
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-&gt;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-&gt;obj</span></code> will be a new reference to the root
1439
 
object.</li>
1440
 
</ul>
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>
1455
 
</dd></dl>
1456
 
 
1457
 
<dl class="member">
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>
1462
 
</pre></div>
1463
 
</div>
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>
1471
 
</ol>
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-&gt;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>
1481
 
</dd></dl>
1482
 
 
1483
 
</div>
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>
1488
 
</div>
1489
 
<dl class="type">
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>
1500
 
</pre></div>
1501
 
</div>
1502
 
</dd></dl>
1503
 
 
1504
 
<dl class="member">
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>
1509
 
</pre></div>
1510
 
</div>
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>
1514
 
</dd></dl>
1515
 
 
1516
 
<dl class="member">
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>
1521
 
</pre></div>
1522
 
</div>
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>
1526
 
</dd></dl>
1527
 
 
1528
 
<dl class="member">
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>
1533
 
</pre></div>
1534
 
</div>
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>
1537
 
</dd></dl>
1538
 
 
1539
 
</div>
1540
 
 
1541
 
 
1542
 
          </div>
1543
 
        </div>
1544
 
      </div>
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>
1548
 
  <ul>
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>
1555
 
</ul>
1556
 
 
1557
 
  <h4>Previous topic</h4>
1558
 
  <p class="topless"><a href="structures.html"
1559
 
                        title="previous chapter">Common Object Structures</a></p>
1560
 
  <h4>Next topic</h4>
1561
 
  <p class="topless"><a href="gcsupport.html"
1562
 
                        title="next chapter">Supporting Cyclic Garbage Collection</a></p>
1563
 
<h3>This Page</h3>
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>
1568
 
</ul>
1569
 
 
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" />
1577
 
    </form>
1578
 
    <p class="searchtip" style="font-size: 90%">
1579
 
    Enter search terms or a module, class or function name.
1580
 
    </p>
1581
 
</div>
1582
 
<script type="text/javascript">$('#searchbox').show(0);</script>
1583
 
        </div>
1584
 
      </div>
1585
 
      <div class="clearer"></div>
1586
 
    </div>  
1587
 
    <div class="related" role="navigation" aria-label="related navigation">
1588
 
      <h3>Navigation</h3>
1589
 
      <ul>
1590
 
        <li class="right" style="margin-right: 10px">
1591
 
          <a href="../genindex.html" title="General Index"
1592
 
             >index</a></li>
1593
 
        <li class="right" >
1594
 
          <a href="../py-modindex.html" title="Python Module Index"
1595
 
             >modules</a> |</li>
1596
 
        <li class="right" >
1597
 
          <a href="gcsupport.html" title="Supporting Cyclic Garbage Collection"
1598
 
             >next</a> |</li>
1599
 
        <li class="right" >
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> &raquo;</li>
1605
 
        <li>
1606
 
          <span class="version_switcher_placeholder">3.5.1</span>
1607
 
          <a href="../index.html">Documentation </a> &raquo;
1608
 
        </li>
1609
 
 
1610
 
          <li class="nav-item nav-item-1"><a href="index.html" >Python/C API Reference Manual</a> &raquo;</li>
1611
 
          <li class="nav-item nav-item-2"><a href="objimpl.html" >Object Implementation Support</a> &raquo;</li> 
1612
 
      </ul>
1613
 
    </div>  
1614
 
    <div class="footer">
1615
 
    &copy; <a href="../copyright.html">Copyright</a> 1990-2016, Python Software Foundation.
1616
 
    <br />
1617
 
    The Python Software Foundation is a non-profit corporation.
1618
 
    <a href="https://www.python.org/psf/donations/">Please donate.</a>
1619
 
    <br />
1620
 
    Last updated on Jan 22, 2016.
1621
 
    <a href="../bugs.html">Found a bug</a>?
1622
 
    <br />
1623
 
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.3.3.
1624
 
    </div>
1625
 
 
1626
 
  </body>
1627
 
</html>
 
 
b'\\ No newline at end of file'