Multiple Inheritance __slots__ problem

Alex Martelli aleaxit at yahoo.com
Fri Dec 24 08:48:05 EST 2004


Nitin Shukla <nitinshukla at infotech.stph.net> wrote:

> Hello,
> 
> Can anyone tell why am I getting this error and how to work around this
> problem.
> 
> >>> class Klass(object):
> ...   __slots__ = ( 'x' )
> ...
> >>> class Klass1(object):
> ...   __slots__ = ( 'y' )
> ...
> >>> class Klass(Klass, Klass1):
> ...   __slots__ = ( 'z' )
> ...
> Traceback (most recent call last):
>   File "<interactive input>", line 1, in ?
> TypeError: multiple bases have instance lay-out conflict

You're getting this error because there is no way to merge the layout of
those slots.

> I need to define slots in these classes and also need to inherit them in
> Derived class.

You never NEED to define slots anywhere: it's just a memory
optimization, that's all there is to it -- you're saving the size of a
dict per instance, a few tens of bytes per instance.  Just remove the
slots all around: how many millions of instances of these classes do you
have alive at the same time?  Identify the leaf class which has the many
millions of instances and reinstate slots there, only, if you really
truly can't spare the RAM.


Alex



More information about the Python-list mailing list