[Tutor] slots and inheritance - a bit more
Arthur Siegel
ajs@ix.netcom.com
Thu, 4 Jul 2002 14:16:39 -0400
Tim writes -
> Slots are inherited. What have you seen that made you believe they're not
> inherited?
>
> >>> class A(object):
> ... __slots__ = 'a'
> ...
> >>> class B(A):
> ... pass
> ...
> >>> b = B()
> >>> b.a = 12
> >>> b.x = 42
> >>> b.a
> 12
> >>> b.x
> 42
> >>> b.__dict__ # only 'x' is in the dict; 'a' is stored in inherited slot
> {'x': 42}
> >>>
Helps.
But the *functionality* of slots is not inherited - unless I kill B's
__dict__. Correct? If so, is that doable?
> BTW, note that __slots__ are really an optimization gimmick. There's no
> reason to bother with them unless you have a great many instances of some
> class and need to reduce memory use. Then they can help, but then you
also
> have to live with the restrictions the use of __slots__ impose.
I kind of thought the restriction - as in object.kolor = "blue" when you
meant
object.color= "blue" was of the essence of slots. But if you mean the
multi-heritance restriction - yes. Happens to be a deal killer in my case.
Art