Skip to content

Commit dabe938

Browse files
committed
add __base__ explaination into Doc/c-api/object.rst
1 parent 53bd224 commit dabe938

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

Doc/c-api/object.rst

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,56 @@ Object Protocol
307307
class, are considered classes. However, objects can override this by having
308308
a :attr:`~class.__bases__` attribute (which must be a tuple of base classes).
309309
310+
Another function, :attr:`__base__` that is specific to CPython and also
311+
exists in Jython and PyPy can also be used on a class inheriting from one
312+
or more classes.
313+
314+
``__base__`` corresponds to the :c:member:`~PyTypeObject.tp_base` in a
315+
type object. At the C level, Python has a single inheritance model
316+
that determines the memory layout of instances. There is a chain
317+
involving base classes that contribute to the instance layout.
318+
``__base__`` is the base class that is involved in that chain.
319+
When such a class takes arguments in the correct order, then starting leftmost.
320+
321+
Let's look at the example cases;
322+
323+
>>> class A(object): pass
324+
...
325+
>>> class B(A): pass
326+
...
327+
>>> class C(int): pass
328+
...
329+
330+
The first user-defined class that either inherits from the instance
331+
of a built-in type other than object or inherits from another user
332+
defined class (single or multiple inheritance) that does so or in the
333+
absence of the above class.
334+
335+
>>> class D(B, A, C): pass
336+
...
337+
>>> D.__base__
338+
<class 'C'>
339+
>>>
340+
341+
A built-in type that is not an object or in the absence of the above class.
342+
343+
>>> class D(B, A, int): pass
344+
...
345+
>>> D.__base__
346+
<class 'int'>
347+
348+
The first user defined class that inherits either an object or
349+
derives from a class (directly or indirectly) that inherits an
350+
object is the value returned by the :attr:`__base__` function.
351+
352+
>>> class D(B, A): pass
353+
...
354+
>>> D.__base__
355+
<class 'B'>
356+
357+
.. impl-detail::
358+
Note that behavior of the ``__base__`` attribute is dependent on the :term:`CPython` implementation.
359+
310360
311361
.. c:function:: int PyObject_IsInstance(PyObject *inst, PyObject *cls)
312362

0 commit comments

Comments
 (0)