__slots__

Alex Martelli aleaxit at yahoo.com
Sat Mar 25 10:57:47 EST 2006


David Isaac <aisaac0 at verizon.net> wrote:
   ...
> Does this beg the question of whether __slots__
> *should* break with inheritance?

How would you expect the following code to behave:

class Base(object):
    def __init__(self): self.x = 23

class Derived(Base):
    __slots__ = 'y',

?  I would expect it to work (as it does in Python), therefore I think
it's fit and proper that __slots__ "breaks with inheritance", meaning it
basically has no effect unless every class in the inheritance DAG has
slots.


> One other question I did not get answered:  is there any
> simple example of a Pythonic use of __slots__ that does NOT
> involve the creation of **many** instances.

Since the only benefit of __slots__ is saving a few bytes per instance,
it's not worth the bother unless there are many instances -- so, the
answer is 'no'.


Alex



More information about the Python-list mailing list