Text of docs.python.org › uk › 3 › c-api › type.html as Styx crawled it on Nov 19, 2026. The page may have changed since. Only its words are kept, without pictures or layout, and only the first part of a long page.

Open the live page

Об’єкти типу — Python 3.15.2 documentation

Навігація індекс модулі | наступний | попередній | Python » 3.15.2 documentation » Python/C API reference manual » Шар бетонних об’єктів » Об’єкти типу | Theme | Об’єкти типу ¶ type PyTypeObject ¶ Part of the Stable ABI (as an opaque struct). Структура C об’єктів, що використовуються для опису вбудованих типів.

PyTypeObject PyType_Type ¶ Part of the Stable ABI . Це об’єкт типу для об’єктів типу; це той самий об’єкт, що й type на рівні Python. int PyType_Check ( PyObject * o ) ¶ Повертає ненульове значення, якщо об’єкт o є об’єктом типу, включаючи екземпляри типів, похідних від об’єкта стандартного типу.

Повертає 0 у всіх інших випадках. Ця функція завжди успішна. int PyType_CheckExact ( PyObject * o ) ¶ Повертає відмінне від нуля значення, якщо об’єкт o є об’єктом типу, але не підтипом об’єкта стандартного типу. Повертає 0 у всіх інших випадках. Ця функція завжди успішна. unsigned int PyType_ClearCache ( ) ¶ Part of the Stable ABI .

Очистіть внутрішній кеш пошуку. Повернути тег поточної версії. unsigned long PyType_GetFlags ( PyTypeObject * type ) ¶ Part of the Stable ABI . Return the 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 tp_flags itself is not part of the limited API .

Added in version 3.2. Змінено в версії 3.4: Тип повернення тепер unsigned long , а не long . PyObject * PyType_GetDict ( PyTypeObject * type ) ¶ Return value: New reference. Return the type object’s internal namespace, which is otherwise only exposed via a read-only proxy ( cls.__dict__ ). This is a replacement for accessing 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 PyObject_GetAttr() ) isn’t adequate. Extension modules should continue to use tp_dict , directly or indirectly, when setting up their own types. Added in version 3.12. void PyType_Modified ( PyTypeObject * type ) ¶ Part of the Stable ABI . Визнати недійсним внутрішній кеш пошуку для типу та всіх його підтипів.

Цю функцію необхідно викликати після будь-якої ручної зміни атрибутів або базових класів типу. int PyType_AddWatcher ( PyType_WatchCallback callback ) ¶ Register callback as a type watcher. Return a non-negative integer ID which must be passed to future calls to PyType_Watch() . In case of error (e.g. no more watcher IDs available), return -1 and set an exception.

In free-threaded builds, PyType_AddWatcher() is not thread-safe, so it must be called at start up (before spawning the first thread). Added in version 3.12. int PyType_ClearWatcher ( int watcher_id ) ¶ Clear watcher identified by watcher_id (previously returned from PyType_AddWatcher() ). Return 0 on success, -1 on error (e.g. if watcher_id was never registered.) An extension should never call PyType_ClearWatcher with a watcher_id that was not returned to it by a previous call to PyType_AddWatcher() .

Added in version 3.12. int PyType_Watch ( int watcher_id , PyObject * type ) ¶ Mark type as watched. The callback granted watcher_id by PyType_AddWatcher() will be called whenever PyType_Modified() reports a change to type . (The callback may be called only once for a series of consecutive modifications to type , if _PyType_Lookup() is not called on type between the modifications; this is an implementation detail and subject to change.) The callback is also invoked when a watched heap type is deallocated.

An extension should never call PyType_Watch with a watcher_id that was not returned to it by a previous call to PyType_AddWatcher() . Added in version 3.12. Змінено в версії 3.15: The callback is now also invoked when a watched heap type is deallocated. int PyType_Unwatch ( int watcher_id , PyObject * type ) ¶ Mark type as not watched. This undoes a previous call to PyType_Watch() . type must not be NULL .

An extension should never call this function with a watcher_id that was not returned to it by a previous call to PyType_AddWatcher() . On success, this function returns 0 . On failure, this function returns -1 with an exception set. Added in version 3.12. typedef int ( * PyType_WatchCallback ) ( PyObject * type ) ¶ Type of a type-watcher callback function. The callback must not modify type or cause PyType_Modified() to be called on type or any type in its MRO; violating this rule could cause infinite recursion.

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. Added in version 3.12. Змінено в версії 3.15: The callback may now be called during deallocation of a watched heap type. int PyType_HasFeature ( PyTypeObject * o , int feature ) ¶ Повертає ненульове значення, якщо об’єкт типу o встановлює функцію feature .

Функції типу позначаються однобітовими прапорцями. int PyType_FastSubclass ( PyTypeObject * type , int flag ) ¶ Return non-zero if the type object type sets the subclass flag flag . Subclass flags are denoted by Py_TPFLAGS_*_SUBCLASS . This function is used by many _Check functions for common types. Дивись також PyObject_TypeCheck() , which is used as a slower alternative in _Check functions for types that don’t come with subclass flags. int PyType_IS_GC ( PyTypeObject * o ) ¶ Return true if the type object includes support for the cycle detector; this tests the type flag Py_TPFLAGS_HAVE_GC . int PyType_IsSubtype ( PyTypeObject * a , PyTypeObject * b ) ¶ Part of the Stable ABI .

Повертає true, якщо a є підтипом b . This function only checks for actual subtypes, which means that __subclasscheck__() is not called on b . Call PyObject_IsSubclass() to do the same check that issubclass() would do. PyObject * PyType_GenericAlloc ( PyTypeObject * type , Py_ssize_t nitems ) ¶ Return value: New reference. Part of the Stable ABI . Generic handler for the 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 PyObject_Init() or PyObject_InitVar() . Do not call this directly to allocate memory for an object; call the type’s tp_alloc slot instead. For types that support garbage collection (i.e., the Py_TPFLAGS_HAVE_GC flag is set), this function behaves like PyObject_GC_New or PyObject_GC_NewVar (except the memory is guaranteed to be zeroed before initialization), and should be paired with PyObject_GC_Del() in tp_free .

Otherwise, it behaves like PyObject_New or PyObject_NewVar (except the memory is guaranteed to be zeroed before initialization) and should be paired with PyObject_Free() in tp_free . PyObject * PyType_GenericNew ( PyTypeObject * type , PyObject * args , PyObject * kwds ) ¶ Return value: New reference. Part of the Stab

The rest of this page is on the live site.