__slots__

Alex Martelli aleaxit at yahoo.com
Sat Mar 25 12:42:07 EST 2006


Ron Garret <rNOSPAMon at flownet.com> wrote:

> In article <1hcqwk5.9z7uv51pwbdrbN%aleaxit at yahoo.com>,
>  aleaxit at yahoo.com (Alex Martelli) wrote:
> 
> > > 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'.
> 
> I can think of at least two other benefits to using __slots__:
> 
> 1.  If you have a typo in an attribute assignment you get an exception
> instead of a latent downstream bug.

If your unittests are so feeble that they won't catch such typos, you
have far bigger problems -- and you should be using pychecker or pylint
anyway, as they'll catch far more typos than __slots__ ever will (far
from all, of course -- a simple typo of + vs - can still kill you --
which is why they can't *substitute* for unittests in any case).

> 2.  Implicit documentation.

If you don't document what the sundry variables are FOR, you're really
not documenting your code at all -- just listing the names of some
attributes is far too weak.  If the existence of such listing can in any
way give the programmer an excuse to NOT do real documentation (as your
classifying it as "implicit documentation" strongly suggests), then the
net effet is not a benefit, but a serious detriment to code quality.


Alex



More information about the Python-list mailing list