__slots__
Duncan Booth
duncan.booth at invalid.invalid
Sat Mar 25 13:12:02 EST 2006
Ron Garret wrote:
> 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.
Only if all classes in the inheritance define __slots__, and don't include
__dict__ in their slots. In particular, it means you can never rely on
this in any code you write which inherits from a library class over which
you have no control.
In other words, it is such a risky thing to depend on that you would be
much better never to rely on it. Try writing some unit tests instead.
>
> 2. Implicit documentation.
Explicit is better than implicit.
More information about the Python-list
mailing list