__slot__: what is it good for?

Peter Otten __peter__ at web.de
Mon Jun 21 11:04:34 EDT 2010


Alexander Eisenhuth wrote:

> - what is the reason, that __slots__ are introduced in python?

When you have "many" instances of a class with a fixed set of attributes 
__slots__ can save you some memory because it avoids the overhead for the 
instance __dict__. Note that "many" means millions rather than thousands. 
The best approach is to measure the memory footprint of your app with and 
without __slots__ in the class you see as a good candidate and only use it 
when you find a significant difference.

> - I want to use slots to define a class where no attributes are added at
> runtime. Is that a good idea to use slots for that?

No.

Peter




More information about the Python-list mailing list