Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f6d6dc2

Browse files
committed
Clean up the docs of PyObject_IsSubclass and PyObject_IsInstance, and mention that they call the PEP 3119 methods.
1 parent a920b6d commit f6d6dc2

2 files changed

Lines changed: 35 additions & 30 deletions

File tree

Doc/c-api/object.rst

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -187,40 +187,45 @@ Object Protocol
187187
a TypeError is raised when *o* is an integer instead of a zero-initialized
188188
bytes object.
189189
190+
191+
.. c:function:: int PyObject_IsSubclass(PyObject *derived, PyObject *cls)
192+
193+
Return ``1`` if the class *derived* is identical to or derived from the class
194+
*cls*, otherwise return ``0``. In case of an error, return ``-1``.
195+
196+
If *cls* is a tuple, the check will be done against every entry in *cls*.
197+
The result will be ``1`` when at least one of the checks returns ``1``,
198+
otherwise it will be ``0``.
199+
200+
If *cls* has a :meth:`~class.__subclasscheck__` method, it will be called to
201+
determine the subclass status as described in :pep:`3119`. Otherwise,
202+
*derived* is a subclass of *cls* if it is a direct or indirect subclass,
203+
i.e. contained in ``cls.__mro__``.
204+
205+
Normally only class objects, i.e. instances of :class:`type` or a derived
206+
class, are considered classes. However, objects can override this by haivng
207+
a :attr:`__bases__` attribute (which must be a tuple of base classes).
208+
209+
190210
.. c:function:: int PyObject_IsInstance(PyObject *inst, PyObject *cls)
191211
192-
Returns ``1`` if *inst* is an instance of the class *cls* or a subclass of
193-
*cls*, or ``0`` if not. On error, returns ``-1`` and sets an exception. If
194-
*cls* is a type object rather than a class object, :c:func:`PyObject_IsInstance`
195-
returns ``1`` if *inst* is of type *cls*. If *cls* is a tuple, the check will
196-
be done against every entry in *cls*. The result will be ``1`` when at least one
197-
of the checks returns ``1``, otherwise it will be ``0``. If *inst* is not a
198-
class instance and *cls* is neither a type object, nor a class object, nor a
199-
tuple, *inst* must have a :attr:`~instance.__class__` attribute --- the
200-
class relationship of the value of that attribute with *cls* will be used
201-
to determine the result of this function.
202-
203-
204-
Subclass determination is done in a fairly straightforward way, but includes a
205-
wrinkle that implementors of extensions to the class system may want to be aware
206-
of. If :class:`A` and :class:`B` are class objects, :class:`B` is a subclass of
207-
:class:`A` if it inherits from :class:`A` either directly or indirectly. If
208-
either is not a class object, a more general mechanism is used to determine the
209-
class relationship of the two objects. When testing if *B* is a subclass of
210-
*A*, if *A* is *B*, :c:func:`PyObject_IsSubclass` returns true. If *A* and *B*
211-
are different objects, *B*'s :attr:`~class.__bases__` attribute is searched in
212-
a depth-first fashion for *A* --- the presence of the :attr:`~class.__bases__`
213-
attribute is considered sufficient for this determination.
212+
Return ``1`` if *inst* is an instance of the class *cls* or a subclass of
213+
*cls*, or ``0`` if not. On error, returns ``-1`` and sets an exception.
214214
215+
If *cls* is a tuple, the check will be done against every entry in *cls*.
216+
The result will be ``1`` when at least one of the checks returns ``1``,
217+
otherwise it will be ``0``.
215218
216-
.. c:function:: int PyObject_IsSubclass(PyObject *derived, PyObject *cls)
219+
If *cls* has a :meth:`~class.__instancecheck__` method, it will be called to
220+
determine the subclass status as described in :pep:`3119`. Otherwise, *inst*
221+
is an instance of *cls* if its class is a subclass of *cls*.
222+
223+
An instance *inst* can override what is considered its class by having a
224+
:attr:`__class__` attribute.
217225
218-
Returns ``1`` if the class *derived* is identical to or derived from the class
219-
*cls*, otherwise returns ``0``. In case of an error, returns ``-1``. If *cls*
220-
is a tuple, the check will be done against every entry in *cls*. The result will
221-
be ``1`` when at least one of the checks returns ``1``, otherwise it will be
222-
``0``. If either *derived* or *cls* is not an actual class object (or tuple),
223-
this function uses the generic algorithm described above.
226+
An object *cls* can override if it is considered a class, and what its base
227+
classes are, by having a :attr:`__bases__` attribute (which must be a tuple
228+
of base classes).
224229
225230
226231
.. c:function:: int PyCallable_Check(PyObject *o)

Doc/c-api/type.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ Type Objects
6969
Return true if *a* is a subtype of *b*.
7070
7171
This function only checks for actual subtypes, which means that
72-
:meth:`~type.__subclasscheck__` is not called on *b*. Call
72+
:meth:`~class.__subclasscheck__` is not called on *b*. Call
7373
:c:func:`PyObject_IsSubclass` to do the same check that :func:`issubclass`
7474
would do.
7575

0 commit comments

Comments
 (0)