Useful? __slots__ for functions

A. Lloyd Flanagan alloydflanagan at comcast.net
Wed May 26 18:18:02 EDT 2004


Jean Brouwers <mrjean1 at comcast.net> wrote in message news:<240520042236356939%mrjean1 at comcast.net>...
[...]
> 
> In addition to being faster (to create) and smaller, __slots__ objects
> have another major advantage, at least in my view.  They are 'frozen'
> and can not be extended dynamically, not intentionally and -even more
> importantly- not by accident.  There are situations where the latter is
> a major benefit, for example in applications where extensibility is not
> a requirement.
> 
> /Jean Brouwers
>  ProphICy Semiconductor, Inc.

I would have to consider that a _major_ disadvantage!

There are a number of good reasons why users of a class might want to
add attributes dynamically.  You can implement all sorts of useful
global mechanisms, like logging, by just adding them to classes or
instances.

Unless you've got a class that uses __slots__.  Then all these schemes
fail miserably.  This makes your class way less useful.  I would never
use __slots__ except as a last resort for performance enhancement
(which is what it was added for).



More information about the Python-list mailing list