Skip to content

Commit df3297b

Browse files
committed
Explain __base__ attribute in the docs
1 parent 3690688 commit df3297b

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

Doc/c-api/object.rst

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,46 @@ Object Protocol
245245
class, are considered classes. However, objects can override this by having
246246
a :attr:`__bases__` attribute (which must be a tuple of base classes).
247247
248+
Another function, :attr:`__base__` that is specific to Cpython and also
249+
exists in Jython and PyPy can also be used on a class inheriting from one
250+
or more classes. When such a class takes arguments in the correct order,
251+
then starting leftmost.
252+
253+
Let's look at the example cases;
254+
255+
>>> class A(object): pass
256+
...
257+
>>> class B(A): pass
258+
...
259+
>>> class C(int): pass
260+
...
261+
262+
The first user-defined class that either inherits from the instance
263+
of a built-in type other than object or inherits from another user
264+
defined class (single or multiple inheritance) that does so or in the
265+
absence of the above class.
266+
267+
>>> class D(B, A, C): pass
268+
...
269+
>>> D.__base__
270+
<class '__main__.C'>
271+
>>>
272+
273+
A built-in type that is not an object or in the absence of the above class.
274+
275+
>>> class D(B, A, int): pass
276+
...
277+
>>> D.__base__
278+
<type 'int'>
279+
280+
The first user defined class that inherits either an object or
281+
derives from a class (directly or indirectly) that inherits an
282+
object is the value returned by the :attr:`__base__` function.
283+
284+
>>> class D(B, A): pass
285+
...
286+
>>> D.__base__
287+
<class '__main__.B'>
248288
249289
.. c:function:: int PyObject_IsInstance(PyObject *inst, PyObject *cls)
250290

0 commit comments

Comments
 (0)