__slots_ and inheritance
Graham Ashton
graham at effectif.com
Mon Apr 14 14:33:53 EDT 2003
On Sat, 12 Apr 2003 15:53:38 +0000, Alex Martelli wrote:
>> Alexander Schmolck <a.schmolck at gmx.net> wrote in message
>> news:<yfsistmh4ec.fsf at black132.ex.ac.uk>...
>>
>>> __slots__ is the one addition in python2.2 that really annoys me. It
>>> seems an ill-conceived jumble of conceptually unrelated things (bug
>>> avoidance, optimization, safety, encapsulation) that screws up
>>> reflection big time for little apparent gain. As Knuth said ...
>
> I think it's an important memory optimization, PERIOD. The other
> conceptually unrelated things (bug avoidance -- I can't even SEE the
> others!) aren't in __slots__' own design (even though a commentator
> who's usually quite perceptive seems to have seen the "bug avoidance"
> feature that wasn't really there).
>From http://www.python.org/doc/2.2.1/whatsnew/sect-rellinks.html:
Finally, it's possible to constrain the list of attributes that can be
referenced on an object using the new __slots__ class attribute.
Python objects are usually very dynamic; at any time it's possible to
define a new attribute on an instance by just doing obj.new_attr=1.
This is flexible and convenient, but this flexibility can also lead to
bugs, as when you meant to write obj.template = 'a' but made a typo
and wrote obj.templtae by accident.
A new-style class can define a class attribute named __slots__ to
constrain the list of legal attribute names.
This thread has pointed out rather well that __slots__ don't really behave
as suggested above in anything but the simplest of situations. It wasn't
until I read the __slots__ section in the nutshell book that I realised
that this wasn't the case. Consequently, I think the above paragraphs are
misleading. Shouldn't they be fixed?
--
Graham Ashton
More information about the Python-list
mailing list