attribute access and comparisons of two different objects

Jean Brouwers mrjean1 at comcast.net
Wed Jun 16 06:57:37 CEST 2004


Permit me to comment on this.  Restricted atrtibute access may not be a
feature of __slots__, but combined with the memory savings and run time
improvement, it is another, secondary benefit of __slots__.

Overriding __setattr__ provides restricted access but at a significant
run time cost compared to __slots__ and without the other benefits of
__slots__.  My posting from May 15 shows some figures without
overloading __setattr__. (Google "group:comp.lang.python.* __slots__ vs
__dict__").

Based on other postings in this group there seems to be a legitimate
need for limiting class extensility without incurring a signficant
performance penalty.  For production Python applications the choice
between using __slots__ and overriding __setattr__ is obviously in
favor of the former.

/Jean Brouwers
  ProphICy Semiconductor, Inc.


In article <95aa1afa.0406152008.2d2ddaf8 at posting.google.com>, Michele
Simionato <michele.simionato at poste.it> wrote:

> "Larry Bates" <lbates at swamisoft.com> wrote in message
> news:<V4KdnTIF-4yRZFPdRVn-ug at comcast.com>...
> > 1) In Python 2.3 there is a new __slots__ methodology that
> > does what you want with class attributes.  One must wonder
> > how everyone got by without it for so many years.  I'm not
> > sure I understand the "overhead" issue.  Some code must be
> > executed to determine if an attribute exists or not, why
> > shouldn't it be up to the programmer to write it by
> > overriding __setattr__ method?
> 
> __slots__ should never be used to restrict attribute access;
> they are just a memory saving optimization; you are better off
> not using it if you can. See this recipe:
> 
> http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/252158
> 
> Yes, overriding __setattr__ has a performance overhaud, so
> just do not freeze your attributes! That's the Pythonic solution.
> 
>    Michele Simionato



More information about the Python-list mailing list