[Python-Dev] Make it an error to use __slots__ with classic classes

Guido van Rossum guido at python.org
Sat Aug 9 07:48:54 EDT 2003


> > Raymond Hettinger writes:
> >  > Using __slots__ with a classic class is an error that does
> >  > not readily reveal itself.  For Py2.3.1, I would like to 
> >  > issue a warning, and for Py2.4, I would like to
> >  > raise an exception upon class creation:
> 
> [Mr. Fred]
> > You'll need to explain your motivation a bit more.  Why is it an error
> > to define __slots__ on an old-style class?  (Useless perhaps, but why
> > an error?)
> 
> Everytime I've seen this occur, the author had intended for
> __slots__ to actually be working to save memory by having
> lighter weight objects.  However, since memory usage is
> not immediately visible, the programming error would go
> unnoticed.

Hm.  There are numerous features of new-style classes that don't work
as expected without giving a clear error when accidentally using a
classic class: overriding __getattribute__, properties trapping write
access, MRO dependencies...

Why should __slots__ be special?  Most of the times it is used in a
new style class it is a subtle mistake just as much as when used in a
classic class.

__slots__ is a terrible hack with nasty, hard-to-fathom side effects
that should only be used by programmers at grandmaster and wizard
levels.  Unfortunately it has gained an enormous undeserved popularity
amongst the novices and apprentices, who should know better than to
use this magic incantation casually.

But since we don't have a declaration for programmer experience,
there's no way to forbid or warn about __slots__ when it is misused.

I'd say leave it alone.

--Guido van Rossum (home page: http://www.python.org/~guido/)

(Yes, I'm really back -- my Linux box survived the move and I have
high-speed internet at my new home.  No more posting from starship
using mutt, yee hah!)



More information about the Python-Dev mailing list