__slots__

Patrick Maupin pmaupin at speakeasy.net
Tue Nov 11 23:52:54 EST 2003


Brad Clements wrote:

> But nonetheless I slotted myself into a corner anyway. Now I have to undo
> all those nasty slots statements.
> 
> My advice.. forget __slots__!

In general, I agree.  However, if one is contemplating
performance optimizations by "Pyrexcizing" Python code
and creating new embedded types, one might find that the
use of __slots__ is a viable small step on the path to
"cdef object." (If one is the sort who wants to keep testing
and debugging in actual Python instead of Pyrex until the
very last minute.)

One might also find, while taking this path, that the
use of __slots__ by itself makes SOME applications run
fast enough without going all the way to Pyrex.  At this
point, one might reasonably conclude (despite the
oft-repeated claim that __slots__ are an un-Pythonic
"premature optimization") that the ability to avoid writing
a C extension by merely adding a single line of (basically
non-executable) code to a class or two is unalloyed goodness.

And in the spare time one creates by not writing a C extension,
one may be forgiven for contemplating whether the original
"premature optimization" claim referred to the code which USES
__slots__ (as some seem to interpret), or to the language feature
of __slots__ itself, which is admittedly not as nice as something
like Psyco has the potential to be, but which nonetheless has the
same advantage over some future optimizations that list
comprehensions have over generator expressions, namely that it
is available NOW, in a working and seemingly stable interpreter.

Regards,
Pat




More information about the Python-list mailing list