How does __slots__ avoid a dictionary lookup?

后端 未结 2 769
后悔当初
后悔当初 2020-12-09 17:58

I\'ve heard that __slots__ makes objects faster by avoiding a dictionary lookup. My confusion comes from Python being a dynamic language. In a static language,

相关标签:
2条回答
  • 2020-12-09 18:10

    __slots__ does not (significantly) speed up attribute access:

    >>> class Foo(object):
    ...     __slots__ = ('spam',)
    ...     def __init__(self):
    ...         self.spam = 'eggs'
    ... 
    >>> class Bar(object):
    ...     def __init__(self):
    ...         self.spam = 'eggs'
    ... 
    >>> import timeit
    >>> timeit.timeit('t.spam', 'from __main__ import Foo; t=Foo()')
    0.07030296325683594
    >>> timeit.timeit('t.spam', 'from __main__ import Bar; t=Bar()')
    0.07646608352661133
    

    The goal of using __slots__ is to save memory; instead of using a .__dict__ mapping on the instance, the class has descriptors objects for each and every attribute named in __slots__ and instances have the attribute assigned wether or not they have an actual value:

    >>> class Foo(object):
    ...     __slots__ = ('spam',)
    ... 
    >>> dir(Foo())
    ['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__slots__', '__str__', '__subclasshook__', 'spam']
    >>> Foo().spam
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: spam
    >>> Foo.spam
    <member 'spam' of 'Foo' objects>
    >>> type(Foo.spam)
    <type 'member_descriptor'>
    

    So python still has to look at the class for each attribute access on an instance of Foo (to find the descriptor). Any unknown attribute (say, Foo.ham) will still result in Python looking through the class MRO to search for that attribute, and that includes dictionary searches. And you can still assign additional attributes to the class:

    >>> Foo.ham = 'eggs'
    >>> dir(Foo)
    ['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__slots__', '__str__', '__subclasshook__', 'ham', 'spam']
    >>> Foo().ham
    'eggs'
    

    The slot descriptors are created when the class is created, and access memory assigned to each instance to store and retrieve a reference to the associated value (the same chunk of memory that tracks instance reference counts and a reference back to the class object). Without slots, a descriptor for __dict__ is used accessing a reference to a dict object in the same manner.

    0 讨论(0)
  • 2020-12-09 18:22

    It might speed up a program where you instantiate lots of objects of the same class, genuinely never change what attributes they have, and cache misses on all those duplicate dictionaries present a real performance problem.

    This is really just a special case of the general situation where saving space sometimes saves time as well, where cache is the limiting factor.

    So, it probably won't make accessing one object faster, but may speed up accessing many objects of the same type.

    See also this question.

    0 讨论(0)
提交回复
热议问题