[Python-Dev] Re: __slots__ and default values

Michael Chermside mcherm@mcherm.com
Tue, 13 May 2003 08:25:18 -0700


Raymond Hettinger wrote:
> class Pane(object):
>     __slots__ = ('background', 'foreground', 'size', 'content')
>     background = 'black'
>     foreground = 'white'
>     size = (80, 25)
...which doesn't work since the class variable overwrites the
__slots__ descriptor.


Aahz replies:
> Why not do the initializing in __init__?

I presume that Raymond's concern was not that there wouldn't be
a way to do initialization, but that this would become a new c.l.p
FAQ and point of confusion for newbies. Unfortunately, I fear
that it will. Already I am seeing that people are "discovering"
class variables as a sort of "initialized instance variable"
instead of using __init__ as they "ought" to. Of course, it's NOT
an initialized instance variable, but newbies stumble across it
and seem to prefer it to using __init__.

Combine this with the fact that newbies from staticly typed 
languages tend to think of __slots__ as "practically mandatory" 
(because it prevents the use of instance variables not pre-declared,
which they erroniously think is a good thing) rather than the 
special purpose performance hack that it REALLY is, and you have
a recipe for trouble.

I'm not quite sure how to present things so as to steer them
right, but there's definitely a potential pitfall here.

-- Michael Chermside