[Tutor] Protected methods/variables

Kent Johnson kent37 at tds.net
Tue Apr 4 21:25:05 CEST 2006


Mike Hansen wrote:
>>> - you can use __slots__ to restrict arbirtrary creation of 
>> (dynamic) 
>>> instrance attributes
>> You can do this, but it is generally considered a misuse of 
>> __slots__ and potentially problematic.
>>
> 
> I'll bite. What is the proper/intended use of __slots__? Does it have
> something to do with memory?

Yes, it is intended specifically to reduce memory consumption of objects 
that are going to be instantiated a lot. I'm not sure how many counts as 
a lot, but in the thousands at least. Using __slots__ saves the cost of 
the attribute dict for each instance.

Kent



More information about the Tutor mailing list