Type Objects — Python 3.11.16 documentation

docs.python.org

Tin mới

Common Object Structures

Supporting Cyclic Garbage Collection

Python Module Index

3.11.16 Documentation

Python/C API Reference Manual

Object Implementation Support

PyTypeObject Definition¶

The structure definition for PyTypeObject can be found in Include/object.h. For convenience of reference, this repeats the definition found there:

configure --with-trace-refs option

These fields are only present when the macro Py_TRACE_REFS is defined (see the configure --with-trace-refs option).

definition.__name__

For dynamically allocated type objects, this should just be the type name, and the module name explicitly stored in the type dict as the value for key '__module__'.

vectorcall protocol

An optional offset to a per-instance function that implements calling the object using the vectorcall protocol, a more efficient alternative of the simpler tp_call.

asynchronous iterator

Pointer to an additional structure that contains fields relevant only to objects which implement awaitable and asynchronous iterator protocols at the C-level. See Async Object Structures for details.

PyObject_HashNotImplemented()

An optional pointer to a function that implements the built-in function hash().

collections.abc.Mapping

This bit indicates that instances of the class may match mapping patterns when used as the subject of a match block. It is automatically set when registering or subclassing collections.abc.Mapping, and unset when registe

gc: Interface to the cycle-detecting garbage collector.

An optional pointer to a traversal function for the garbage collector. This is only used if the Py_TPFLAGS_HAVE_GC flag bit is set. The signature is:

PyObject_ClearWeakRefs()

If the instances of this type are weakly referenceable, this field is greater than zero and contains the offset in the instance structure of the weak reference list head (ignoring the GC header, if present); this offset

PySequence_GetSlice()

This function is used by PyObject_GetItem() and PySequence_GetSlice(), and has the same signature as PyObject_GetItem(). This slot must be filled for the PyMapping_Check() function to return 1, it can be NULL otherwise.

Buffer request types

Handle a request to exporter to fill in view as specified by flags. Except for point (3), an implementation of this function MUST take these steps:

Defining Extension Types: Tutorial

The following are simple examples of Python type definitions. They include common usage you may encounter. Some demonstrate tricky corner cases. For more examples, practical info, and a tutorial, see Defining Extension T

Defining Extension Types: Assorted Topics

The following are simple examples of Python type definitions. They include common usage you may encounter. Some demonstrate tricky corner cases. For more examples, practical info, and a tutorial, see Defining Extension T