Objek Tipe — Dokumentasi Python 3.15.2
Navigasi index modul | berikut | sebelum | Python » 3.15.2 documentation » Python/C API reference manual » Lapisan Objek Konkrit » Objek Tipe | Tema | Objek Tipe ¶ type PyTypeObject ¶ Part of the Stable ABI (as an opaque struct). The C structure of the objects used to describe built-in types. PyTypeObject PyType_Type ¶ Part of the Stable ABI . This is the type object for type objects; it is the same object as type in the Python layer. int PyType_Check ( PyObject * o ) ¶ 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. int PyType_CheckExact ( PyObject * o ) ¶ 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. unsigned int PyType_ClearCache ( ) ¶ Part of the Stable ABI . Clear the internal lookup cache. Return the current version tag. 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. Berubah pada versi 3.4: The return type is now unsigned long rather than 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 . 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 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. Berubah pada versi 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. Berubah pada versi 3.15: The callback may now be called during deallocation of a watched heap type. int PyType_HasFeature ( PyTypeObject * o , int feature ) ¶ Return non-zero if the type object o sets the feature feature .
Type features are denoted by single bit flags. 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. Lihat juga 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 .
Return true if a is a subtype of 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 Stable ABI . Generic handler for the tp_new slot of a type object. Creates a new instance using the type's tp_alloc slot and returns the resulting object. int PyType_Ready ( PyTypeObject * type ) ¶ Part of the Stable ABI . 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. Catatan If some of the base classes implements the GC protocol and the provided type does not include the 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 Py_TPFLAGS_HAVE_GC in its flags then it must implement the GC protocol itself by at least implementing the tp_traverse handle.
PyObject * PyType_GetName ( PyTypeObject * type ) ¶ Return
The rest of this page is on the live site.