__base__ and __bases__[0]

Jeremy Hylton jeremy at zope.com
Mon Apr 28 18:52:24 CEST 2003

On Mon, 2003-04-28 at 12:13, Michele Simionato wrote:
> I thought __base__ and __bases__[0] where just equivalent, but it
> seems it is not so:
> >>> class C(object): pass
> ...
> >>> class M(C,type): pass
> ...
> >>> M.__bases__[0]
> <class '__main__.C'>
> >>> M.__base__
> <type 'type'>
> Question: how __base__ is computed and what's the rationale about it ?

__base__ corresponds to the tp_base slot in a type object.  At the C
level, Python has a single inheritance model that determines the memory
layout of instances.  There is a chain involving base classes that
contribute to the instance layout.  __base__ is the base class that is
involved in that chain.

It's possible that there are multiple base classes and none of them
contribute to the layout.  This would occur if each of the base classes
is a plain old Python class.  In that case, I think the choice is of
__base__ is arbitrary.

Here's a concrete example:

class A(object): pass
class B(dict): pass
class C(A, B): pass

C.__base__ is B

because B's base class defines a custom instance layout.

BTW, I don't think this is documented anywhere.  I have to ask Guido


More information about the Python-list mailing list