Python object overhead?

Jean-Paul Calderone exarkun at divmod.com
Sat Mar 24 16:19:43 EDT 2007


On 24 Mar 2007 13:08:02 -0700, 7stud <bbxx789_05ss at yahoo.com> wrote:
>On Mar 23, 4:04 pm, Jack Diederich <jackd... at jackdied.com> wrote:
>>
>> If you make the record a new style class (inherit from object) you can
>> specify the __slots__ attribute on the class.  This eliminates the per
>> instance dictionary overhead in exchange for less flexibility.
>>
>
>How is efficiency improved with __slots__?  Don't you create a list
>object that replaces the dictionary object, e.g.:
>
>class Test(object):
>        __slots__ = ["m", "n"]
>

Only one list is created.  It is used to define a C array where attributes
will be stored.  Each instance still has that C array, but it has much less
overhead than a Python list or dictionary.

Whether this reduction in overhead actually results in a useful or measurable
performance improvement is a separate question, of course.

Jean-Paul




More information about the Python-list mailing list