“Python/docs/3.9/c-api/type”的版本间差异

来自菜鸟教程
Python/docs/3.9/c-api/type
跳转至:导航、​搜索
(autoload)
 
(Page commit)
 
第1行: 第1行:
 +
{{DISPLAYTITLE:类型对象 — Python 文档}}
 
<div id="type-objects" class="section">
 
<div id="type-objects" class="section">
  
 
<span id="typeobjects"></span>
 
<span id="typeobjects"></span>
= Type Objects =
+
= 类型对象 =
  
 
<span id="index-0" class="target"></span>
 
<span id="index-0" class="target"></span>
; ''type'' <code>PyTypeObject</code>
+
; <span class="k"><span class="pre">type</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyTypeObject</span></span></span><br />
: The C structure of the objects used to describe built-in types.
 
  
; [[../structures#c|PyObject]] *<code>PyType_Type</code>
+
: 用于描述内置类型的对象的 C 结构。
: This is the type object for type objects; it is the same object as [[../../library/functions#type|<code>type</code>]] in the Python layer.
 
  
; int <code>PyType_Check</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''o''<span class="sig-paren">)</span>
+
; [[#c.PyTypeObject|<span class="n"><span class="pre">PyTypeObject</span></span>]]<span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyType_Type</span></span></span><br />
: Return non-zero if the object ''o'' is a type object, including instances of types derived from the standard type object. Return 0 in all other cases.
 
  
; int <code>PyType_CheckExact</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''o''<span class="sig-paren">)</span>
+
: 这是类型对象的类型对象; 它与 Python 层中的 [[../../library/functions#type|type]] 对象相同。
: Return non-zero if the object ''o'' is a type object, but not a subtype of the standard type object. Return 0 in all other cases.
 
  
; unsigned int <code>PyType_ClearCache</code><span class="sig-paren">(</span><span class="sig-paren">)</span>
+
; <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyType_Check</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">o</span></span><span class="sig-paren">)</span><br />
: Clear the internal lookup cache. Return the current version tag.
+
 
 +
: 如果对象 ''o'' 是类型对象,包括从标准类型对象派生的类型实例,则返回非零值。 在所有其他情况下返回 0。 此功能总是成功。
 +
 
 +
; <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyType_CheckExact</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">o</span></span><span class="sig-paren">)</span><br />
 +
 
 +
: 如果对象 ''o'' 是类型对象,但不是标准类型对象的子类型,则返回非零值。 在所有其他情况下返回 0。 此功能总是成功。
 +
 
 +
; <span class="kt"><span class="pre">unsigned</span> <span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyType_ClearCache</span></span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><br />
 +
 
 +
: 清除内部查找缓存。 返回当前版本标签。
  
 
<dl>
 
<dl>
<dt>unsigned long <code>PyType_GetFlags</code><span class="sig-paren">(</span>[[#c.PyTypeObject|PyTypeObject]] *''type''<span class="sig-paren">)</span></dt>
+
<dt><span class="kt"><span class="pre">unsigned</span> <span class="pre">long</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyType_GetFlags</span></span></span><span class="sig-paren">(</span>[[#c.PyTypeObject|<span class="n"><span class="pre">PyTypeObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">type</span></span><span class="sig-paren">)</span><br />
<dd><p>Return the [[../typeobj#c.PyTypeObject|<code>tp_flags</code>]] member of ''type''. This function is primarily
+
</dt>
meant for use with Py_LIMITED_API; the individual flag bits are
+
<dd><p>返回 ''type'' 的 [[../typeobj#c.PyTypeObject|tp_flags]] 成员。 此函数主要用于 Py_LIMITED_API; 各个标志位保证在 Python 版本中稳定,但对 [[../typeobj#c.PyTypeObject|tp_flags]] 本身的访问不是受限 API 的一部分。</p>
guaranteed to be stable across Python releases, but access to
 
[[../typeobj#c.PyTypeObject|<code>tp_flags</code>]] itself is not part of the limited API.</p>
 
 
<div class="versionadded">
 
<div class="versionadded">
  
<p><span class="versionmodified added">3.2 新版功能.</span></p>
+
<p><span class="versionmodified added">3.2 版中的新功能。</span></p>
  
 
</div>
 
</div>
 
<div class="versionchanged">
 
<div class="versionchanged">
  
<p><span class="versionmodified changed">3.4 版更改: </span>The return type is now <code>unsigned long</code> rather than <code>long</code>.</p>
+
<p><span class="versionmodified changed"> 3.4 版更改: </span> 返回类型现在是 <code>unsigned long</code> 而不是 <code>long</code></p>
  
 
</div></dd></dl>
 
</div></dd></dl>
  
; void <code>PyType_Modified</code><span class="sig-paren">(</span>[[#c.PyTypeObject|PyTypeObject]] *''type''<span class="sig-paren">)</span>
+
; <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyType_Modified</span></span></span><span class="sig-paren">(</span>[[#c.PyTypeObject|<span class="n"><span class="pre">PyTypeObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">type</span></span><span class="sig-paren">)</span><br />
: Invalidate the internal lookup cache for the type and all of its subtypes. This function must be called after any manual modification of the attributes or base classes of the type.
+
 
 +
: 使该类型及其所有子类型的内部查找缓存无效。 在对该类型的属性或基类进行任何手动修改后,必须调用此函数。
  
; int <code>PyType_HasFeature</code><span class="sig-paren">(</span>[[#c.PyTypeObject|PyTypeObject]] *''o'', int ''feature''<span class="sig-paren">)</span>
+
; <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyType_HasFeature</span></span></span><span class="sig-paren">(</span>[[#c.PyTypeObject|<span class="n"><span class="pre">PyTypeObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">o</span></span>, <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="n"><span class="pre">feature</span></span><span class="sig-paren">)</span><br />
: Return non-zero if the type object ''o'' sets the feature ''feature''. Type features are denoted by single bit flags.
 
  
; int <code>PyType_IS_GC</code><span class="sig-paren">(</span>[[#c.PyTypeObject|PyTypeObject]] *''o''<span class="sig-paren">)</span>
+
: 如果类型对象 ''o'' 设置了特征 ''特征'' ,则返回非零值。 类型特征由单个位标志表示。
: Return true if the type object includes support for the cycle detector; this tests the type flag [[../typeobj#Py_TPFLAGS_HAVE_GC|<code>Py_TPFLAGS_HAVE_GC</code>]].
+
 
 +
; <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyType_IS_GC</span></span></span><span class="sig-paren">(</span>[[#c.PyTypeObject|<span class="n"><span class="pre">PyTypeObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">o</span></span><span class="sig-paren">)</span><br />
 +
 
 +
: 如果类型对象包含对循环检测器的支持,则返回 true; 这将测试类型标志 [[../typeobj#Py_TPFLAGS_HAVE_GC|Py_TPFLAGS_HAVE_GC]]
  
 
<dl>
 
<dl>
<dt>int <code>PyType_IsSubtype</code><span class="sig-paren">(</span>[[#c.PyTypeObject|PyTypeObject]] *''a'', [[#c.PyTypeObject|PyTypeObject]] *''b''<span class="sig-paren">)</span></dt>
+
<dt><span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyType_IsSubtype</span></span></span><span class="sig-paren">(</span>[[#c.PyTypeObject|<span class="n"><span class="pre">PyTypeObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">a</span></span>, [[#c.PyTypeObject|<span class="n"><span class="pre">PyTypeObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">b</span></span><span class="sig-paren">)</span><br />
<dd><p>Return true if ''a'' is a subtype of ''b''.</p>
+
</dt>
<p>This function only checks for actual subtypes, which means that
+
<dd><p>如果 ''a'' ''b'' 的子类型,则返回 true。</p>
[[../../reference/datamodel#class|<code>__subclasscheck__()</code>]] is not called on ''b''. Call
+
<p>这个函数只检查实际的子类型,这意味着在 ''b'' 上不会调用 [[../../reference/datamodel#class|__subclasscheck__()]]。 调用 [[../object#c|PyObject_IsSubclass()]] 进行与 [[../../library/functions#issubclass|issubclass()]] 相同的检查。</p></dd></dl>
[[../object#c|<code>PyObject_IsSubclass()</code>]] to do the same check that [[../../library/functions#issubclass|<code>issubclass()</code>]]
+
 
would do.</p></dd></dl>
+
; [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyType_GenericAlloc</span></span></span><span class="sig-paren">(</span>[[#c.PyTypeObject|<span class="n"><span class="pre">PyTypeObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">type</span></span>, <span class="n"><span class="pre">Py_ssize_t</span></span><span class="w"> </span><span class="n"><span class="pre">nitems</span></span><span class="sig-paren">)</span><br />
 +
 
 +
: 类型对象的 [[../typeobj#c.PyTypeObject|tp_alloc]] 槽的通用处理程序。 使用Python默认的内存分配机制来分配一个新实例,并将其所有内容初始化为<code>NULL</code>
 +
 
 +
; [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyType_GenericNew</span></span></span><span class="sig-paren">(</span>[[#c.PyTypeObject|<span class="n"><span class="pre">PyTypeObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">type</span></span>, [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span>, [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">kwds</span></span><span class="sig-paren">)</span><br />
  
<dl>
+
: 类型对象的 [[../typeobj#c.PyTypeObject|tp_new]] 槽的通用处理程序。 使用类型的 [[../typeobj#c.PyTypeObject|tp_alloc]] 槽创建一个新实例。
<dt>[[../structures#c|PyObject]] *<code>PyType_GenericAlloc</code><span class="sig-paren">(</span>[[#c.PyTypeObject|PyTypeObject]] *''type'', Py_ssize_t ''nitems''<span class="sig-paren">)</span></dt>
 
<dd><p>''Return value: New reference.''</p>
 
<p>Generic handler for the [[../typeobj#c.PyTypeObject|<code>tp_alloc</code>]] slot of a type object. Use
 
Python's default memory allocation mechanism to allocate a new instance and
 
initialize all its contents to <code>NULL</code>.</p></dd></dl>
 
  
 
<dl>
 
<dl>
<dt>[[../structures#c|PyObject]] *<code>PyType_GenericNew</code><span class="sig-paren">(</span>[[#c.PyTypeObject|PyTypeObject]] *''type'', [[../structures#c|PyObject]] *''args'', [[../structures#c|PyObject]] *''kwds''<span class="sig-paren">)</span></dt>
+
<dt><span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyType_Ready</span></span></span><span class="sig-paren">(</span>[[#c.PyTypeObject|<span class="n"><span class="pre">PyTypeObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">type</span></span><span class="sig-paren">)</span><br />
<dd><p>''Return value: New reference.''</p>
+
</dt>
<p>Generic handler for the [[../typeobj#c.PyTypeObject|<code>tp_new</code>]] slot of a type object. Create a
+
<dd><p>完成一个类型对象。 这应该在所有类型对象上调用以完成它们的初始化。 该函数负责从类型的基类添加继承的槽。 成功返回 <code>0</code>,或返回 <code>-1</code> 并在错误时设置异常。</p>
new instance using the type's [[../typeobj#c.PyTypeObject|<code>tp_alloc</code>]] slot.</p></dd></dl>
+
<div class="admonition note">
  
; int <code>PyType_Ready</code><span class="sig-paren">(</span>[[#c.PyTypeObject|PyTypeObject]] *''type''<span class="sig-paren">)</span>
+
<p>笔记</p>
: Finalize a type object. This should be called on all type objects to finish their initialization. This function is responsible for adding inherited slots from a type's base class. Return <code>0</code> on success, or return <code>-1</code> and sets an exception on error.
+
<p>如果某些基类实现了 GC 协议,并且提供的类型在其标志中不包含 [[../typeobj#Py_TPFLAGS_HAVE_GC|Py_TPFLAGS_HAVE_GC]],则 GC 协议将从其父类自动实现。 相反,如果正在创建的类型确实在其标志中包含 [[../typeobj#Py_TPFLAGS_HAVE_GC|Py_TPFLAGS_HAVE_GC]],那么它 '''必须''' 至少通过实现 [[../typeobj#c.PyTypeObject|tp_traverse]] 句柄来实现 GC 协议本身。</p>
 +
 
 +
</div></dd></dl>
  
 
<dl>
 
<dl>
<dt>void *<code>PyType_GetSlot</code><span class="sig-paren">(</span>[[#c.PyTypeObject|PyTypeObject]] *''type'', int ''slot''<span class="sig-paren">)</span></dt>
+
<dt><span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyType_GetSlot</span></span></span><span class="sig-paren">(</span>[[#c.PyTypeObject|<span class="n"><span class="pre">PyTypeObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">type</span></span>, <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="n"><span class="pre">slot</span></span><span class="sig-paren">)</span><br />
<dd><p>Return the function pointer stored in the given slot. If the
+
</dt>
result is <code>NULL</code>, this indicates that either the slot is <code>NULL</code>,
+
<dd><p>返回存储在给定槽中的函数指针。 如果结果是 <code>NULL</code>,这表明插槽是 <code>NULL</code>,或者是使用无效参数调用了该函数。 调用者通常会将结果指针转换为适当的函数类型。</p>
or that the function was called with invalid parameters.
+
<p>有关 ''slot'' 参数的可能值,请参阅 <code>PyType_Slot.slot</code></p>
Callers will typically cast the result pointer into the appropriate
+
<p>如果 ''type'' 不是堆类型,则会引发异常。</p>
function type.</p>
 
<p>See <code>PyType_Slot.slot</code> for possible values of the ''slot'' argument.</p>
 
<p>An exception is raised if ''type'' is not a heap type.</p>
 
 
<div class="versionadded">
 
<div class="versionadded">
  
<p><span class="versionmodified added">3.4 新版功能.</span></p>
+
<p><span class="versionmodified added">3.4 版中的新功能。</span></p>
  
 
</div></dd></dl>
 
</div></dd></dl>
  
 
<dl>
 
<dl>
<dt>[[../structures#c|PyObject]] *<code>PyType_GetModule</code><span class="sig-paren">(</span>[[#c.PyTypeObject|PyTypeObject]] *''type''<span class="sig-paren">)</span></dt>
+
<dt>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyType_GetModule</span></span></span><span class="sig-paren">(</span>[[#c.PyTypeObject|<span class="n"><span class="pre">PyTypeObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">type</span></span><span class="sig-paren">)</span><br />
<dd><p>Return the module object associated with the given type when the type was
+
</dt>
created using [[#c.PyType_FromModuleAndSpec|<code>PyType_FromModuleAndSpec()</code>]].</p>
+
<dd><p>当使用 [[#c.PyType_FromModuleAndSpec|PyType_FromModuleAndSpec()]] 创建类型时,返回与给定类型关联的模块对象。</p>
<p>If no module is associated with the given type, sets [[../../library/exceptions#TypeError|<code>TypeError</code>]]
+
<p>如果没有模块与给定类型关联,则设置 [[../../library/exceptions#TypeError|TypeError]] 并返回 <code>NULL</code></p>
and returns <code>NULL</code>.</p>
+
<p>该函数通常用于获取定义方法的模块。 请注意,在这种方法中, <code>PyType_GetModule(Py_TYPE(self))</code> 可能不会返回预期的结果。 <code>Py_TYPE(self)</code> 可能是预期类的 ''子类'' ,子类不一定定义在与其超类相同的模块中。 请参阅 [[../structures#c|PyCMethod]] 以获取定义该方法的类。</p>
<p>This function is usually used to get the module in which a method is defined.
 
Note that in such a method, <code>PyType_GetModule(Py_TYPE(self))</code>
 
may not return the intended result.
 
<code>Py_TYPE(self)</code> may be a ''subclass'' of the intended class, and subclasses
 
are not necessarily defined in the same module as their superclass.
 
See [[../structures#c|<code>PyCMethod</code>]] to get the class that defines the method.</p>
 
 
<div class="versionadded">
 
<div class="versionadded">
  
<p><span class="versionmodified added">3.9 新版功能.</span></p>
+
<p><span class="versionmodified added">3.9 版中的新功能。</span></p>
  
 
</div></dd></dl>
 
</div></dd></dl>
  
 
<dl>
 
<dl>
<dt>void *<code>PyType_GetModuleState</code><span class="sig-paren">(</span>[[#c.PyTypeObject|PyTypeObject]] *''type''<span class="sig-paren">)</span></dt>
+
<dt><span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyType_GetModuleState</span></span></span><span class="sig-paren">(</span>[[#c.PyTypeObject|<span class="n"><span class="pre">PyTypeObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">type</span></span><span class="sig-paren">)</span><br />
<dd><p>Return the state of the module object associated with the given type.
+
</dt>
This is a shortcut for calling [[../module#c|<code>PyModule_GetState()</code>]] on the result
+
<dd><p>返回与给定类型关联的模块对象的状态。 这是在 [[#c.PyType_GetModule|PyType_GetModule()]] 的结果上调用 [[../module#c|PyModule_GetState()]] 的快捷方式。</p>
of [[#c.PyType_GetModule|<code>PyType_GetModule()</code>]].</p>
+
<p>如果没有模块与给定类型关联,则设置 [[../../library/exceptions#TypeError|TypeError]] 并返回 <code>NULL</code></p>
<p>If no module is associated with the given type, sets [[../../library/exceptions#TypeError|<code>TypeError</code>]]
+
<p>如果 ''type'' 有关联模块,但其状态为 <code>NULL</code>,则返回 <code>NULL</code>,不设置异常。</p>
and returns <code>NULL</code>.</p>
 
<p>If the ''type'' has an associated module but its state is <code>NULL</code>,
 
returns <code>NULL</code> without setting an exception.</p>
 
 
<div class="versionadded">
 
<div class="versionadded">
  
<p><span class="versionmodified added">3.9 新版功能.</span></p>
+
<p><span class="versionmodified added">3.9 版中的新功能。</span></p>
  
 
</div></dd></dl>
 
</div></dd></dl>
第120行: 第116行:
 
<div id="creating-heap-allocated-types" class="section">
 
<div id="creating-heap-allocated-types" class="section">
  
== Creating Heap-Allocated Types ==
+
== 创建堆分配类型 ==
  
The following functions and structs are used to create
+
以下函数和结构用于创建 [[../typeobj#heap-types|堆类型]]
[[../typeobj#heap-types|<span class="std std-ref">heap types</span>]].
 
  
 
<dl>
 
<dl>
<dt>[[../structures#c|PyObject]] *<code>PyType_FromModuleAndSpec</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''module'', [[#c.PyType_Spec|PyType_Spec]] *''spec'', [[../structures#c|PyObject]] *''bases''<span class="sig-paren">)</span></dt>
+
<dt>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyType_FromModuleAndSpec</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">module</span></span>, [[#c.PyType_Spec|<span class="n"><span class="pre">PyType_Spec</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">spec</span></span>, [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">bases</span></span><span class="sig-paren">)</span><br />
<dd><p>''Return value: New reference.''</p>
+
</dt>
<p>Creates and returns a heap type object from the ''spec''
+
<dd><p>''spec'' ([[../typeobj#Py_TPFLAGS_HEAPTYPE|Py_TPFLAGS_HEAPTYPE]]) 创建并返回堆类型对象。</p>
([[../typeobj#Py_TPFLAGS_HEAPTYPE|<code>Py_TPFLAGS_HEAPTYPE</code>]]).</p>
+
<p>如果 ''bases'' 是元组,则创建的堆类型包含其中包含的所有类型作为基类型。</p>
<p>If ''bases'' is a tuple, the created heap type contains all types contained
+
<p>如果 ''bases'' <code>NULL</code>,则使用 ''Py_tp_bases'' 插槽。 如果这也是 <code>NULL</code>,则使用 ''Py_tp_base'' 插槽。 如果这也是 <code>NULL</code>,则新类型派生自 [[../../library/functions#object|object]]</p>
in it as base types.</p>
+
<p>''module'' 参数可用于记录定义新类的模块。 它必须是一个模块对象或 <code>NULL</code>。 如果不是 <code>NULL</code>,则模块与新类型相关联,稍后可以使用 [[#c.PyType_GetModule|PyType_GetModule()]] 检索。 关联的模块不被子类继承; 必须为每个类单独指定它。</p>
<p>If ''bases'' is <code>NULL</code>, the ''Py_tp_bases'' slot is used instead.
+
<p>此函数在新类型上调用 [[#c.PyType_Ready|PyType_Ready()]]</p>
If that also is <code>NULL</code>, the ''Py_tp_base'' slot is used instead.
 
If that also is <code>NULL</code>, the new type derives from [[../../library/functions#object|<code>object</code>]].</p>
 
<p>The ''module'' argument can be used to record the module in which the new
 
class is defined. It must be a module object or <code>NULL</code>.
 
If not <code>NULL</code>, the module is associated with the new type and can later be
 
retreived with [[#c.PyType_GetModule|<code>PyType_GetModule()</code>]].
 
The associated module is not inherited by subclasses; it must be specified
 
for each class individually.</p>
 
<p>This function calls [[#c.PyType_Ready|<code>PyType_Ready()</code>]] on the new type.</p>
 
 
<div class="versionadded">
 
<div class="versionadded">
  
<p><span class="versionmodified added">3.9 新版功能.</span></p>
+
<p><span class="versionmodified added">3.9 版中的新功能。</span></p>
  
 
</div></dd></dl>
 
</div></dd></dl>
  
 
<dl>
 
<dl>
<dt>[[../structures#c|PyObject]] *<code>PyType_FromSpecWithBases</code><span class="sig-paren">(</span>[[#c.PyType_Spec|PyType_Spec]] *''spec'', [[../structures#c|PyObject]] *''bases''<span class="sig-paren">)</span></dt>
+
<dt>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyType_FromSpecWithBases</span></span></span><span class="sig-paren">(</span>[[#c.PyType_Spec|<span class="n"><span class="pre">PyType_Spec</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">spec</span></span>, [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">bases</span></span><span class="sig-paren">)</span><br />
<dd><p>''Return value: New reference.''</p>
+
</dt>
<p>Equivalent to <code>PyType_FromModuleAndSpec(NULL, spec, bases)</code>.</p>
+
<dd><p>相当于 <code>PyType_FromModuleAndSpec(NULL, spec, bases)</code></p>
 
<div class="versionadded">
 
<div class="versionadded">
  
<p><span class="versionmodified added">3.3 新版功能.</span></p>
+
<p><span class="versionmodified added">3.3 版中的新功能。</span></p>
  
 
</div></dd></dl>
 
</div></dd></dl>
  
<dl>
+
; [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyType_FromSpec</span></span></span><span class="sig-paren">(</span>[[#c.PyType_Spec|<span class="n"><span class="pre">PyType_Spec</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">spec</span></span><span class="sig-paren">)</span><br />
<dt>[[../structures#c|PyObject]] *<code>PyType_FromSpec</code><span class="sig-paren">(</span>[[#c.PyType_Spec|PyType_Spec]] *''spec''<span class="sig-paren">)</span></dt>
+
 
<dd><p>''Return value: New reference.''</p>
+
: 相当于 <code>PyType_FromSpecWithBases(spec, NULL)</code>
<p>Equivalent to <code>PyType_FromSpecWithBases(spec, NULL)</code>.</p></dd></dl>
 
  
 
<dl>
 
<dl>
<dt>''type'' <code>PyType_Spec</code></dt>
+
<dt><span class="k"><span class="pre">type</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyType_Spec</span></span></span><br />
<dd><p>Structure defining a type's behavior.</p>
+
</dt>
 +
<dd><p>定义类型行为的结构。</p>
 
<dl>
 
<dl>
<dt>''const'' char *<code>PyType_Spec.</code><code>name</code></dt>
+
<dt><span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-prename descclassname"><span class="n"><span class="pre">PyType_Spec</span></span><span class="p"><span class="pre">.</span></span></span><span class="sig-name descname"><span class="n"><span class="pre">name</span></span></span><br />
<dd><p>Name of the type, used to set [[../typeobj#c.PyTypeObject|<code>PyTypeObject.tp_name</code>]].</p></dd></dl>
+
</dt>
 +
<dd><p>类型名称,用于设置[[../typeobj#c.PyTypeObject|PyTypeObject.tp_name]]</p></dd></dl>
  
 
<dl>
 
<dl>
<dt>int <code>PyType_Spec.</code><code>basicsize</code></dt>
+
<dt><span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-prename descclassname"><span class="n"><span class="pre">PyType_Spec</span></span><span class="p"><span class="pre">.</span></span></span><span class="sig-name descname"><span class="n"><span class="pre">basicsize</span></span></span><br />
 +
</dt>
 
<dd></dd></dl>
 
<dd></dd></dl>
  
 
<dl>
 
<dl>
<dt>int <code>PyType_Spec.</code><code>itemsize</code></dt>
+
<dt><span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-prename descclassname"><span class="n"><span class="pre">PyType_Spec</span></span><span class="p"><span class="pre">.</span></span></span><span class="sig-name descname"><span class="n"><span class="pre">itemsize</span></span></span><br />
<dd><p>Size of the instance in bytes, used to set
+
</dt>
[[../typeobj#c.PyTypeObject|<code>PyTypeObject.tp_basicsize</code>]] and
+
<dd><p>实例的大小(以字节为单位),用于设置 [[../typeobj#c.PyTypeObject|PyTypeObject.tp_basicsize]] [[../typeobj#c.PyTypeObject|PyTypeObject.tp_itemsize]]</p></dd></dl>
[[../typeobj#c.PyTypeObject|<code>PyTypeObject.tp_itemsize</code>]].</p></dd></dl>
 
  
 
<dl>
 
<dl>
<dt>int <code>PyType_Spec.</code><code>flags</code></dt>
+
<dt><span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-prename descclassname"><span class="n"><span class="pre">PyType_Spec</span></span><span class="p"><span class="pre">.</span></span></span><span class="sig-name descname"><span class="n"><span class="pre">flags</span></span></span><br />
<dd><p>Type flags, used to set [[../typeobj#c.PyTypeObject|<code>PyTypeObject.tp_flags</code>]].</p>
+
</dt>
<p>If the <code>Py_TPFLAGS_HEAPTYPE</code> flag is not set,
+
<dd><p>类型标志,用于设置[[../typeobj#c.PyTypeObject|PyTypeObject.tp_flags]]</p>
[[#c.PyType_FromSpecWithBases|<code>PyType_FromSpecWithBases()</code>]] sets it automatically.</p></dd></dl>
+
<p>如果未设置 <code>Py_TPFLAGS_HEAPTYPE</code> 标志,则 [[#c.PyType_FromSpecWithBases|PyType_FromSpecWithBases()]] 会自动设置它。</p></dd></dl>
  
 
<dl>
 
<dl>
<dt>[[#c.PyType_Slot|PyType_Slot]] *<code>PyType_Spec.</code><code>slots</code></dt>
+
<dt>[[#c.PyType_Slot|<span class="n"><span class="pre">PyType_Slot</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-prename descclassname"><span class="n"><span class="pre">PyType_Spec</span></span><span class="p"><span class="pre">.</span></span></span><span class="sig-name descname"><span class="n"><span class="pre">slots</span></span></span><br />
<dd><p>Array of [[#c.PyType_Slot|<code>PyType_Slot</code>]] structures.
+
</dt>
Terminated by the special slot value <code>{0, NULL}</code>.</p></dd></dl>
+
<dd><p>[[#c.PyType_Slot|PyType_Slot]] 结构数组。 由特殊槽值 <code>{0, NULL}</code> 终止。</p></dd></dl>
 
</dd></dl>
 
</dd></dl>
  
 
<dl>
 
<dl>
<dt>''type'' <code>PyType_Slot</code></dt>
+
<dt><span class="k"><span class="pre">type</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyType_Slot</span></span></span><br />
<dd><p>Structure defining optional functionality of a type, containing a slot ID
+
</dt>
and a value pointer.</p>
+
<dd><p>定义类型的可选功能的结构,包含插槽 ID 和值指针。</p>
 
<dl>
 
<dl>
<dt>int <code>PyType_Slot.</code><code>slot</code></dt>
+
<dt><span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-prename descclassname"><span class="n"><span class="pre">PyType_Slot</span></span><span class="p"><span class="pre">.</span></span></span><span class="sig-name descname"><span class="n"><span class="pre">slot</span></span></span><br />
 +
</dt>
 
<dd><blockquote><div>
 
<dd><blockquote><div>
  
<p>A slot ID.</p>
+
<p>插槽 ID。</p>
<p>Slot IDs are named like the field names of the structures
+
<p>插槽 ID 的命名类似于结构 [[#c.PyTypeObject|PyTypeObject、]][[../typeobj#c|PyNumberMethods]][[../typeobj#c|PySequenceMethods]][[../typeobj#c|PyMappingMethods]] 和 [X168syncA]PyMappingMethods[X1653X]PyMappingMethods[X1653X]PyMappingMethods]添加了 <code>Py_</code> 前缀。 例如,使用:</p>
[[#c.PyTypeObject|<code>PyTypeObject</code>]], [[../typeobj#c|<code>PyNumberMethods</code>]],
 
[[../typeobj#c|<code>PySequenceMethods</code>]], [[../typeobj#c|<code>PyMappingMethods</code>]] and
 
[[../typeobj#c|<code>PyAsyncMethods</code>]] with an added <code>Py_</code> prefix.
 
For example, use:</p>
 
 
<ul>
 
<ul>
<li><p><code>Py_tp_dealloc</code> to set [[../typeobj#c.PyTypeObject|<code>PyTypeObject.tp_dealloc</code>]]</p></li>
+
<li><p><code>Py_tp_dealloc</code> 设置 [[../typeobj#c.PyTypeObject|PyTypeObject.tp_dealloc]]</p></li>
<li><p><code>Py_nb_add</code> to set [[../typeobj#c.PyNumberMethods|<code>PyNumberMethods.nb_add</code>]]</p></li>
+
<li><p><code>Py_nb_add</code> 设置 [[../typeobj#c.PyNumberMethods|PyNumberMethods.nb_add]]</p></li>
<li><p><code>Py_sq_length</code> to set [[../typeobj#c.PySequenceMethods|<code>PySequenceMethods.sq_length</code>]]</p></li></ul>
+
<li><p><code>Py_sq_length</code> 设置 [[../typeobj#c.PySequenceMethods|PySequenceMethods.sq_length]]</p></li></ul>
  
<p>The following fields cannot be set at all using [[#c.PyType_Spec|<code>PyType_Spec</code>]] and
+
<p>使用 [[#c.PyType_Spec|PyType_Spec]] <code>PyType_Slot</code> 根本无法设置以下字段:</p>
<code>PyType_Slot</code>:</p>
 
 
<ul>
 
<ul>
 
<li><p>[[../typeobj#c.PyTypeObject|<code>tp_dict</code>]]</p></li>
 
<li><p>[[../typeobj#c.PyTypeObject|<code>tp_dict</code>]]</p></li>
第220行: 第203行:
 
<li><p>[[../typeobj#c.PyTypeObject|<code>tp_weaklist</code>]]</p></li>
 
<li><p>[[../typeobj#c.PyTypeObject|<code>tp_weaklist</code>]]</p></li>
 
<li><p>[[../typeobj#c.PyTypeObject|<code>tp_vectorcall</code>]]</p></li>
 
<li><p>[[../typeobj#c.PyTypeObject|<code>tp_vectorcall</code>]]</p></li>
<li><p>[[../typeobj#c.PyTypeObject|<code>tp_weaklistoffset</code>]]
+
<li><p>[[../typeobj#c.PyTypeObject|tp_weaklistoffset]](见 [[../structures#pymemberdef-offsets|PyMemberDef]]</p></li>
(see [[../structures#pymemberdef-offsets|<span class="std std-ref">PyMemberDef</span>]])</p></li>
+
<li><p>[[../typeobj#c.PyTypeObject|tp_dictoffset]](见 [[../structures#pymemberdef-offsets|PyMemberDef]]</p></li>
<li><p>[[../typeobj#c.PyTypeObject|<code>tp_dictoffset</code>]]
+
<li><p>[[../typeobj#c.PyTypeObject|tp_vectorcall_offset]](见 [[../structures#pymemberdef-offsets|PyMemberDef]]</p></li></ul>
(see [[../structures#pymemberdef-offsets|<span class="std std-ref">PyMemberDef</span>]])</p></li>
 
<li><p>[[../typeobj#c.PyTypeObject|<code>tp_vectorcall_offset</code>]]
 
(see [[../structures#pymemberdef-offsets|<span class="std std-ref">PyMemberDef</span>]])</p></li></ul>
 
  
<p>The following fields cannot be set using [[#c.PyType_Spec|<code>PyType_Spec</code>]] and
+
<p>受限API下无法使用[[#c.PyType_Spec|PyType_Spec]]<code>PyType_Slot</code>设置以下字段:</p>
<code>PyType_Slot</code> under the limited API:</p>
 
 
<ul>
 
<ul>
 
<li><p>[[../typeobj#c.PyBufferProcs|<code>bf_getbuffer</code>]]</p></li>
 
<li><p>[[../typeobj#c.PyBufferProcs|<code>bf_getbuffer</code>]]</p></li>
 
<li><p>[[../typeobj#c.PyBufferProcs|<code>bf_releasebuffer</code>]]</p></li></ul>
 
<li><p>[[../typeobj#c.PyBufferProcs|<code>bf_releasebuffer</code>]]</p></li></ul>
  
<p>Setting <code>Py_tp_bases</code> or <code>Py_tp_base</code> may be
+
<p>在某些平台上设置 <code>Py_tp_bases</code> <code>Py_tp_base</code> 可能会出现问题。 为避免出现问题,请改用 <code>PyType_FromSpecWithBases()</code> 的 ''bases'' 参数。</p>
problematic on some platforms.
 
To avoid issues, use the ''bases'' argument of
 
<code>PyType_FromSpecWithBases()</code> instead.</p>
 
  
 
</div></blockquote>
 
</div></blockquote>
 
<div class="versionchanged">
 
<div class="versionchanged">
  
<p><span class="versionmodified changed">3.9 版更改: </span>Slots in [[../typeobj#c|<code>PyBufferProcs</code>]] in may be set in the unlimited API.</p>
+
<p><span class="versionmodified changed"> 3.9 版更改: [[../typeobj#c|PyBufferProcs]] 中的 </span> 插槽可以在无限制 API 中设置。</p>
  
 
</div></dd></dl>
 
</div></dd></dl>
  
 
<dl>
 
<dl>
<dt>void *<code>PyType_Slot.</code><code>pfunc</code></dt>
+
<dt><span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-prename descclassname"><span class="n"><span class="pre">PyType_Slot</span></span><span class="p"><span class="pre">.</span></span></span><span class="sig-name descname"><span class="n"><span class="pre">pfunc</span></span></span><br />
<dd><p>The desired value of the slot. In most cases, this is a pointer
+
</dt>
to a function.</p>
+
<dd><p>插槽的所需值。 在大多数情况下,这是一个指向函数的指针。</p>
<p>May not be <code>NULL</code>.</p></dd></dl>
+
<p>可能不是 <code>NULL</code></p></dd></dl>
 
</dd></dl>
 
</dd></dl>
  
  
 
</div>
 
</div>
 +
 +
</div>
 +
<div class="clearer">
 +
 +
  
 
</div>
 
</div>
  
[[Category:Python 3.9 中文文档]]
+
[[Category:Python 3.9 文档]]

2021年10月31日 (日) 04:50的最新版本

类型对象

type PyTypeObject
用于描述内置类型的对象的 C 结构。
PyTypeObject PyType_Type
这是类型对象的类型对象; 它与 Python 层中的 type 对象相同。
int PyType_Check(PyObject *o)
如果对象 o 是类型对象,包括从标准类型对象派生的类型实例,则返回非零值。 在所有其他情况下返回 0。 此功能总是成功。
int PyType_CheckExact(PyObject *o)
如果对象 o 是类型对象,但不是标准类型对象的子类型,则返回非零值。 在所有其他情况下返回 0。 此功能总是成功。
unsigned int PyType_ClearCache()
清除内部查找缓存。 返回当前版本标签。
unsigned long PyType_GetFlags(PyTypeObject *type)

返回 typetp_flags 成员。 此函数主要用于 Py_LIMITED_API; 各个标志位保证在 Python 版本中稳定,但对 tp_flags 本身的访问不是受限 API 的一部分。

3.2 版中的新功能。

3.4 版更改: 返回类型现在是 unsigned long 而不是 long

void PyType_Modified(PyTypeObject *type)
使该类型及其所有子类型的内部查找缓存无效。 在对该类型的属性或基类进行任何手动修改后,必须调用此函数。
int PyType_HasFeature(PyTypeObject *o, int feature)
如果类型对象 o 设置了特征 特征 ,则返回非零值。 类型特征由单个位标志表示。
int PyType_IS_GC(PyTypeObject *o)
如果类型对象包含对循环检测器的支持,则返回 true; 这将测试类型标志 Py_TPFLAGS_HAVE_GC
int PyType_IsSubtype(PyTypeObject *a, PyTypeObject *b)

如果 ab 的子类型,则返回 true。

这个函数只检查实际的子类型,这意味着在 b 上不会调用 __subclasscheck__()。 调用 PyObject_IsSubclass() 进行与 issubclass() 相同的检查。

PyObject *PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems)
类型对象的 tp_alloc 槽的通用处理程序。 使用Python默认的内存分配机制来分配一个新实例,并将其所有内容初始化为NULL
PyObject *PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
类型对象的 tp_new 槽的通用处理程序。 使用类型的 tp_alloc 槽创建一个新实例。
int PyType_Ready(PyTypeObject *type)

完成一个类型对象。 这应该在所有类型对象上调用以完成它们的初始化。 该函数负责从类型的基类添加继承的槽。 成功返回 0,或返回 -1 并在错误时设置异常。

笔记

如果某些基类实现了 GC 协议,并且提供的类型在其标志中不包含 Py_TPFLAGS_HAVE_GC,则 GC 协议将从其父类自动实现。 相反,如果正在创建的类型确实在其标志中包含 Py_TPFLAGS_HAVE_GC,那么它 必须 至少通过实现 tp_traverse 句柄来实现 GC 协议本身。

void *PyType_GetSlot(PyTypeObject *type, int slot)

返回存储在给定槽中的函数指针。 如果结果是 NULL,这表明插槽是 NULL,或者是使用无效参数调用了该函数。 调用者通常会将结果指针转换为适当的函数类型。

有关 slot 参数的可能值,请参阅 PyType_Slot.slot

如果 type 不是堆类型,则会引发异常。

3.4 版中的新功能。

PyObject *PyType_GetModule(PyTypeObject *type)

当使用 PyType_FromModuleAndSpec() 创建类型时,返回与给定类型关联的模块对象。

如果没有模块与给定类型关联,则设置 TypeError 并返回 NULL

该函数通常用于获取定义方法的模块。 请注意,在这种方法中, PyType_GetModule(Py_TYPE(self)) 可能不会返回预期的结果。 Py_TYPE(self) 可能是预期类的 子类 ,子类不一定定义在与其超类相同的模块中。 请参阅 PyCMethod 以获取定义该方法的类。

3.9 版中的新功能。

void *PyType_GetModuleState(PyTypeObject *type)

返回与给定类型关联的模块对象的状态。 这是在 PyType_GetModule() 的结果上调用 PyModule_GetState() 的快捷方式。

如果没有模块与给定类型关联,则设置 TypeError 并返回 NULL

如果 type 有关联模块,但其状态为 NULL,则返回 NULL,不设置异常。

3.9 版中的新功能。

创建堆分配类型

以下函数和结构用于创建 堆类型

PyObject *PyType_FromModuleAndSpec(PyObject *module, PyType_Spec *spec, PyObject *bases)

spec (Py_TPFLAGS_HEAPTYPE) 创建并返回堆类型对象。

如果 bases 是元组,则创建的堆类型包含其中包含的所有类型作为基类型。

如果 basesNULL,则使用 Py_tp_bases 插槽。 如果这也是 NULL,则使用 Py_tp_base 插槽。 如果这也是 NULL,则新类型派生自 object

module 参数可用于记录定义新类的模块。 它必须是一个模块对象或 NULL。 如果不是 NULL,则模块与新类型相关联,稍后可以使用 PyType_GetModule() 检索。 关联的模块不被子类继承; 必须为每个类单独指定它。

此函数在新类型上调用 PyType_Ready()

3.9 版中的新功能。

PyObject *PyType_FromSpecWithBases(PyType_Spec *spec, PyObject *bases)

相当于 PyType_FromModuleAndSpec(NULL, spec, bases)

3.3 版中的新功能。

PyObject *PyType_FromSpec(PyType_Spec *spec)
相当于 PyType_FromSpecWithBases(spec, NULL)
type PyType_Spec

定义类型行为的结构。

const char *PyType_Spec.name

类型名称,用于设置PyTypeObject.tp_name

int PyType_Spec.basicsize
int PyType_Spec.itemsize

实例的大小(以字节为单位),用于设置 PyTypeObject.tp_basicsizePyTypeObject.tp_itemsize

int PyType_Spec.flags

类型标志,用于设置PyTypeObject.tp_flags

如果未设置 Py_TPFLAGS_HEAPTYPE 标志,则 PyType_FromSpecWithBases() 会自动设置它。

PyType_Slot *PyType_Spec.slots

PyType_Slot 结构数组。 由特殊槽值 {0, NULL} 终止。

type PyType_Slot

定义类型的可选功能的结构,包含插槽 ID 和值指针。

int PyType_Slot.slot

插槽 ID。

插槽 ID 的命名类似于结构 PyTypeObject、PyNumberMethodsPySequenceMethodsPyMappingMethods 和 [X168syncA]PyMappingMethods[X1653X]PyMappingMethods[X1653X]PyMappingMethods]添加了 Py_ 前缀。 例如,使用:

使用 PyType_SpecPyType_Slot 根本无法设置以下字段:

受限API下无法使用PyType_SpecPyType_Slot设置以下字段:

在某些平台上设置 Py_tp_basesPy_tp_base 可能会出现问题。 为避免出现问题,请改用 PyType_FromSpecWithBases()bases 参数。

3.9 版更改: PyBufferProcs 中的 插槽可以在无限制 API 中设置。

void *PyType_Slot.pfunc

插槽的所需值。 在大多数情况下,这是一个指向函数的指针。

可能不是 NULL