Teks sumber — English
Terjemahan — Bahasa Indonesia
Type Objects
Objek Tipe
The C structure of the objects used to describe built-in types.
This is the type object for type objects; it is the same object as :class:`type` in the Python layer.
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. This function always succeeds.
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. This function always succeeds.
Return the :c:member:`~PyTypeObject.tp_flags` member of *type*. This function is primarily meant for use with ``Py_LIMITED_API``; the individual flag bits are guaranteed to be stable across Python releases, but access to :c:member:`~PyTypeObject.tp_flags` itself is not part of the :ref:`limited API <limited-c-api>`.
Return the type object's internal namespace, which is otherwise only exposed via a read-only proxy (:attr:`cls.__dict__ <type.__dict__>`). This is a replacement for accessing :c:member:`~PyTypeObject.tp_dict` directly. The returned dictionary must be treated as read-only.
This function is meant for specific embedding and language-binding cases, where direct access to the dict is necessary and indirect access (e.g. via the proxy or :c:func:`PyObject_GetAttr`) isn't adequate.
The callback may be called during type deallocation. In this case, the type object is temporarily resurrected (its reference count is at least 1) and all its attributes are still valid. However, the callback should not store new strong references to the type, as this would resurrect the object and prevent its deallocation.
Return non-zero if the type object *o* sets the feature *feature*. Type features are denoted by single bit flags.
Return non-zero if the type object *type* sets the subclass flag *flag*. Subclass flags are denoted by :c:macro:`Py_TPFLAGS_*_SUBCLASS <Py_TPFLAGS_LONG_SUBCLASS>`. This function is used by many ``_Check`` functions for common types.
:c:func:`PyObject_TypeCheck`, which is used as a slower alternative in ``_Check`` functions for types that don't come with subclass flags.
Return true if the type object includes support for the cycle detector; this tests the type flag :c:macro:`Py_TPFLAGS_HAVE_GC`.
This function only checks for actual subtypes, which means that :meth:`~type.__subclasscheck__` is not called on *b*. Call :c:func:`PyObject_IsSubclass` to do the same check that :func:`issubclass` would do.
Generic handler for the :c:member:`~PyTypeObject.tp_alloc` slot of a type object. Uses Python's default memory allocation mechanism to allocate memory for a new instance, zeros the memory, then initializes the memory as if by calling :c:func:`PyObject_Init` or :c:func:`PyObject_InitVar`.
Do not call this directly to allocate memory for an object; call the type's :c:member:`~PyTypeObject.tp_alloc` slot instead.
For types that support garbage collection (i.e., the :c:macro:`Py_TPFLAGS_HAVE_GC` flag is set), this function behaves like :c:macro:`PyObject_GC_New` or :c:macro:`PyObject_GC_NewVar` (except the memory is guaranteed to be zeroed before initialization), and should be paired with :c:func:`PyObject_GC_Del` in :c:member:`~PyTypeObject.tp_free`. Otherwise, it behaves like :c:macro:`PyObject_New` or :c:macro:`PyObject_NewVar` (except the memory is guaranteed to be zeroed before initialization) and should be paired with :c:func:`PyObject_Free` in :c:member:`~PyTypeObject.tp_free`.
Generic handler for the :c:member:`~PyTypeObject.tp_new` slot of a type object. Creates a new instance using the type's :c:member:`~PyTypeObject.tp_alloc` slot and returns the resulting object.
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 ``0`` on success, or return ``-1`` and sets an exception on error.
If some of the base classes implements the GC protocol and the provided type does not include the :c:macro:`Py_TPFLAGS_HAVE_GC` in its flags, then the GC protocol will be automatically implemented from its parents. On the contrary, if the type being created does include :c:macro:`Py_TPFLAGS_HAVE_GC` in its flags then it **must** implement the GC protocol itself by at least implementing the :c:member:`~PyTypeObject.tp_traverse` handle.
Return the module object associated with the given type when the type was created using :c:func:`PyType_FromModuleAndSpec`.
Return the state of the module object associated with the given type. This is a shortcut for calling :c:func:`PyModule_GetState()` on the result of :c:func:`PyType_GetModule`.
This function is intended to be used together with :c:func:`PyModule_GetState()` to get module state from slot methods (such as :c:member:`~PyTypeObject.tp_init` or :c:member:`~PyNumberMethods.nb_add`) and other places where a method's defining class cannot be passed using the :c:type:`PyCMethod` calling convention.
:ref:`weakrefobjects`
:py:meth:`~object.__new__` is not called on the new class (and it must be set to ``type.__new__``).
:py:meth:`~object.__init__` is not called on the new class.
:py:meth:`~object.__init_subclass__` is not called on any bases.
:py:meth:`~object.__set_name__` is not called on new descriptors.
Slots are typically defined as a global static constant arrays. However, sometimes slot values are not statically known at compile time. For example, slots like :c:data:`Py_tp_bases`, :c:data:`Py_tp_metaclass` and :c:data:`Py_tp_module` require live Python objects. In this case, it is recommended to put such slots on the stack, and use :c:macro:`Py_slot_subslots` to refer to an array of static slots. For example::
static const PySlot my_slots[] = {
PySlot_STATIC_DATA(Py_tp_name, "MyClass"),
PySlot_FUNC(Py_tp_repr, my_repr_func),
...
PySlot_END
};
PyObject *make_my_class(PyObject *module) {
PySlot all_slots[] = {
PySlot_STATIC_DATA(Py_slot_subslots, my_slots),
PySlot_DATA(Py_tp_module, module),
PySlot_END
};
return PyType_FromSlots(all_slots);
}
Most type slot IDs are named like the field names of the structures :c:type:`PyTypeObject`, :c:type:`PyNumberMethods`, :c:type:`PySequenceMethods`, :c:type:`PyMappingMethods` and :c:type:`PyAsyncMethods` with an added ``Py_`` prefix. For example, use:
:c:data:`Py_tp_dealloc` to set :c:member:`PyTypeObject.tp_dealloc`
Additional slots do not directly correspond to a :c:type:`!PyTypeObject` struct field:
:c:member:`~PyTypeObject.tp_weaklistoffset` (use :c:macro:`Py_TPFLAGS_MANAGED_WEAKREF` instead if possible)
:c:member:`~PyTypeObject.tp_dictoffset` (use :c:macro:`Py_TPFLAGS_MANAGED_DICT` instead if possible)
:c:member:`~PyTypeObject.tp_vectorcall_offset` (use ``"__vectorcalloffset__"`` in :ref:`PyMemberDef <pymemberdef-offsets>`)
:c:member:`~PyTypeObject.tp_dict`, :c:member:`~PyTypeObject.tp_mro`, :c:member:`~PyTypeObject.tp_cache`, :c:member:`~PyTypeObject.tp_subclasses`, and :c:member:`~PyTypeObject.tp_weaklist`.
The field :c:member:`~PyTypeObject.tp_vectorcall` can now be set using :c:data:`Py_tp_vectorcall`. See the field's documentation for details.
The :c:data:`Py_tp_bases` slot may be set to a single type object, making it equivalent to the :c:data:`Py_tp_base` slot. Previously, a tuple of types was required.
:c:member:`Slot ID <PySlot.sl_id>` for the name of the type, used to set :c:member:`PyTypeObject.tp_name`.
:c:member:`Slot ID <PySlot.sl_id>` for the size of the instance in bytes. It is used to set :c:member:`PyTypeObject.tp_basicsize`.
This slot may not be used with :c:func:`PyType_GetSlot`. Use :c:member:`PyTypeObject.tp_basicsize` instead if needed, but be aware that a type's size is often considered an implementation detail.
The value is used, together with the size of superclasses, to set :c:member:`PyTypeObject.tp_basicsize`. Python will insert padding as needed to meet :c:member:`!tp_basicsize`'s alignment requirements.
Use :c:func:`PyObject_GetTypeData` to get a pointer to subclass-specific memory reserved this way.
:c:member:`Slot ID <PySlot.sl_id>` for the size of one element of a variable-size type, in bytes. Used to set :c:member:`PyTypeObject.tp_itemsize`. See :c:member:`!tp_itemsize` documentation for caveats.
If this slot is missing, :c:member:`~PyTypeObject.tp_itemsize` is inherited. Extending arbitrary variable-sized classes is dangerous, since some types use a fixed offset for variable-sized memory, which can then overlap fixed-sized memory used by a subclass. To help prevent mistakes, inheriting ``itemsize`` is only possible in the following situations:
The base is not variable-sized (its :c:member:`~PyTypeObject.tp_itemsize` is zero).
:c:member:`Slot ID <PySlot.sl_id>` for type flags, used to set :c:member:`PyTypeObject.tp_flags`.
:c:member:`Slot ID <PySlot.sl_id>` for base types, used to set :c:member:`PyTypeObject.tp_bases`.
The slot can be set to a tuple of type objects which the newly created type should inherit from, like the "positional arguments" of a Python :ref:`class definition <class>`.
Alternately, the slot can be set to a single type object to specify a single base. The effect is the same as specifying a one-element tuple.
:c:member:`Slot ID <PySlot.sl_id>` for the metaclass used to construct the resulting type object. When omitted the metaclass is derived from bases (:c:macro:`Py_tp_bases` or the *bases* argument of :c:func:`PyType_FromMetaclass`).
Metaclasses that override :c:member:`~PyTypeObject.tp_new` are not supported, except if ``tp_new`` is ``NULL``.
This slot may not be used with :c:func:`PyType_GetSlot`. Use :c:func:`Py_TYPE` on the type object instead.
The value must be a module object. The module is associated with the new type and can later be retrieved with :c:func:`PyType_GetModule`. The associated module is not inherited by subclasses; it must be specified for each class individually.
The :c:member:`~PyTypeObject.tp_new` of the metaclass is *ignored*. which may result in incomplete initialization. Creating classes whose metaclass overrides :c:member:`~PyTypeObject.tp_new` is deprecated.
Creating classes whose metaclass overrides :c:member:`~PyTypeObject.tp_new` is no longer allowed.
object
objek