Don't use __slots__ (was Re: performance of dictionary lookup vs. object attributes)

Dieter Maurer dieter at handshake.de
Mon Aug 28 15:26:09 EDT 2006


"Patrick Maupin" <pmaupin at gmail.com> writes on 26 Aug 2006 12:51:44 -0700:
> ...
> The only
> problem I personally know of is that the __slots__ aren't inherited,

"__slots__" *ARE* inherited, although the rules may be a bit
complex.

>>> class B(object):
...   __slots__ = ('f1', 'f2',)
...
>>> class C(B): pass
...
>>> C.__slots__
('f1', 'f2')
>>> c=C()
>>> c.__dict__
{}
>>> c.f1='f1'
>>> c.__dict__
{}
>>> c.fc='fc'
>>> c.__dict__
{'fc': 'fc'}
>>> class C2(B):
...   __slots__=('f21',)
...
>>> C2.__slots__
('f21',)
>>> c2=C2()
>>> c2.f1='x'
>>> c2.f21='y'

--
Dieter



More information about the Python-list mailing list