Multiple Inheritance __slots__ problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nitin Shukla

    Multiple Inheritance __slots__ problem

    Hello,

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

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

    Nitin


  • Alex Martelli

    #2
    Re: Multiple Inheritance __slots__ problem

    Nitin Shukla <nitinshukla@in fotech.stph.net > wrote:
    [color=blue]
    > Hello,
    >
    > Can anyone tell why am I getting this error and how to work around this
    > problem.
    >[color=green][color=darkred]
    > >>> class Klass(object):[/color][/color]
    > ... __slots__ = ( 'x' )
    > ...[color=green][color=darkred]
    > >>> class Klass1(object):[/color][/color]
    > ... __slots__ = ( 'y' )
    > ...[color=green][color=darkred]
    > >>> class Klass(Klass, Klass1):[/color][/color]
    > ... __slots__ = ( 'z' )
    > ...
    > Traceback (most recent call last):
    > File "<interacti ve input>", line 1, in ?
    > TypeError: multiple bases have instance lay-out conflict[/color]

    You're getting this error because there is no way to merge the layout of
    those slots.
    [color=blue]
    > I need to define slots in these classes and also need to inherit them in
    > Derived class.[/color]

    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

    Comment

    Working...