Teks sumber — English
Terjemahan — Bahasa Indonesia
The ``NotImplemented`` singleton, used to signal that an operation is not implemented for the given type combination.
object address : 0x7f80124702c0
object refcount : 2
object type : 0x9902e0
object type name: str
object repr : 'abcdef'
Generic attribute getter function that is meant to be put into a type object's ``tp_getattro`` slot. It looks for a descriptor in the dictionary of classes in the object's MRO as well as an attribute in the object's :attr:`~object.__dict__` (if present). As outlined in :ref:`descriptors`, data descriptors take preference over instance attributes, while non-data descriptors don't. Otherwise, an :exc:`AttributeError` is raised.
Generic attribute setter and deleter function that is meant to be put into a type object's :c:member:`~PyTypeObject.tp_setattro` slot. It looks for a data descriptor in the dictionary of classes in the object's MRO, and if found it takes preference over setting or deleting the attribute in the instance dictionary. Otherwise, the attribute is set or deleted in the object's :attr:`~object.__dict__` (if present). On success, ``0`` is returned, otherwise an :exc:`AttributeError` is raised and ``-1`` is returned.
Compute a bytes representation of object *o*. ``NULL`` is returned on failure and a bytes object on success. This is equivalent to the Python expression ``bytes(o)``, when *o* is not an integer. Unlike ``bytes(o)``, a TypeError is raised when *o* is an integer instead of a zero-initialized bytes object.
If *cls* has a :meth:`~type.__subclasscheck__` method, it will be called to determine the subclass status as described in :pep:`3119`. Otherwise, *derived* is a subclass of *cls* if it is a direct or indirect subclass, i.e. if *cls* is contained in :attr:`derived.__mro__ <type.__mro__>`.
Normally only class objects, i.e. instances of :class:`type` or a derived class, are considered classes. However, objects can override this by having a :attr:`~type.__bases__` attribute (which must be a tuple of base classes).
If *cls* has a :meth:`~type.__instancecheck__` method, it will be called to determine the subclass status as described in :pep:`3119`. Otherwise, *inst* is an instance of *cls* if its class is a subclass of *cls*.
An object *cls* can override if it is considered a class, and what its base classes are, by having a :attr:`~type.__bases__` attribute (which must be a tuple of base classes).
The return type is now Py_hash_t. This is a signed integer the same size as :c:type:`Py_ssize_t`.
Set a :exc:`TypeError` indicating that ``type(o)`` is not :term:`hashable` and return ``-1``. This function receives special treatment when stored in a ``tp_hash`` slot, allowing a type to explicitly indicate to the interpreter that it is not hashable.
When *o* is non-``NULL``, returns a type object corresponding to the object type of object *o*. On failure, raises :exc:`SystemError` and returns ``NULL``. This is equivalent to the Python expression ``type(o)``. This function creates a new :term:`strong reference` to the return value. There's really no reason to use this function instead of the :c:func:`Py_TYPE()` function, which returns a pointer of type :c:expr:`PyTypeObject*`, except when a new :term:`strong reference` is needed.
Return non-zero if the object *o* is of type *type* or a subtype of *type*, and ``0`` otherwise. Both parameters must be non-``NULL``.
This is equivalent to the Python expression ``iter(o)``. It returns a new iterator for the object argument, or the object itself if the object is already an iterator. Raises :exc:`TypeError` and returns ``NULL`` if the object cannot be iterated.
This is equivalent to the Python ``__iter__(self): return self`` method. It is intended for :term:`iterator` types, to be used in the :c:member:`PyTypeObject.tp_iter` slot.
This is the equivalent to the Python expression ``aiter(o)``. Takes an :class:`AsyncIterable` object and returns an :class:`AsyncIterator` for it. This is typically a new iterator but if the argument is an :class:`AsyncIterator`, this returns itself. Raises :exc:`TypeError` and returns ``NULL`` if the object cannot be iterated.
The object *o* must be an instance of *cls*, and *cls* must have been created using negative :c:member:`PyType_Spec.basicsize`. Python does not check this.
Return the size of the instance memory space reserved for *cls*, i.e. the size of the memory :c:func:`PyObject_GetTypeData` returns.
This may be larger than requested using :c:member:`-PyType_Spec.basicsize <PyType_Spec.basicsize>`; it is safe to use this larger size (e.g. with :c:func:`!memset`).
The type *cls* **must** have been created using negative :c:member:`PyType_Spec.basicsize`. Python does not check this.
On error, set an exception and return ``NULL``. :py:exc:`TypeError` is raised if *o* does not have :c:macro:`Py_TPFLAGS_ITEMS_AT_END` set.
This function must only be called in a traverse function of the type which has the :c:macro:`Py_TPFLAGS_MANAGED_DICT` flag set.
This function must only be called in a clear function of the type which has the :c:macro:`Py_TPFLAGS_MANAGED_DICT` flag set.
This function is intended to be used soon after *obj* is created, by the code that creates it, such as in the object's :c:member:`~PyTypeObject.tp_new` slot.
Typically, correct use of this function requires support from *obj*'s deallocator (:c:member:`~PyTypeObject.tp_dealloc`). For example, the following sketch could be adapted to implement a "weakmap" that works like a :py:class:`~weakref.WeakValueDictionary` for a specific type:
PyMutex mutex;
PyObject *
add_entry(weakmap_key_type *key, PyObject *value)
{
PyUnstable_EnableTryIncRef(value);
weakmap_type weakmap = ...;
PyMutex_Lock(&mutex);
weakmap_add_entry(weakmap, key, value);
PyMutex_Unlock(&mutex);
Py_RETURN_NONE;
}
PyObject *
get_value(weakmap_key_type *key)
{
weakmap_type weakmap = ...;
PyMutex_Lock(&mutex);
PyObject *result = weakmap_find(weakmap, key);
if (PyUnstable_TryIncRef(result)) {
// `result` is safe to use
PyMutex_Unlock(&mutex);
return result;
}
// if we get here, `result` is starting to be garbage-collected,
// but has not been removed from the weakmap yet
PyMutex_Unlock(&mutex);
return NULL;
}
// tp_dealloc function for weakmap values
void
value_dealloc(PyObject *value)
{
weakmap_type weakmap = ...;
PyMutex_Lock(&mutex);
weakmap_remove_value(weakmap, value);
...
PyMutex_Unlock(&mutex);
}
This function is intended to be used soon after *op* is created, by the code that creates it, such as in the object's :c:member:`~PyTypeObject.tp_new` slot. Returns 1 if the object was made immortal and returns 0 if it was not. This function cannot fail.
type
tipe