Structures

Duncan Booth duncan.booth at invalid.invalid
Tue Nov 4 14:55:45 CET 2008


Michele Simionato <michele.simionato at gmail.com> wrote:

> I did not expect such a large difference in instantiation time.
> However I was thinking about
> access time, and then the difference is not impressive (~20-25%):
> 

The difference in time is because when you create a normal instance Python 
has to create at least two objects: the instance itself, and a dict for its 
attributes. If you use __slots__ for all of the attributes then you no 
longer have a separate __dict__ so you save that extra object creation.

As soon as you allow other attributes (which happens for example if you 
have a base class without __slots__) I think you'll find the object 
creation time goes back up to normal.

-- 
Duncan Booth http://kupuguy.blogspot.com



More information about the Python-list mailing list