3 ms·
The article mentions, a bit unclearly, that there are two types of slots: the slots used to implement method dispatch in the interpreter and __slots__ [1-2]. Ar
by ejstronge 12y ago
The article mentions, a bit unclearly, that there are two types of slots: the slots used to implement method dispatch in the interpreter and __slots__ [1-2]. Armin's article is about method dispatch.
__slots__ are useful to reduce memory usage when you're instantiating lots (tens of thousands or millions) of Python objects. Since objects defined in Python code support the addition of arbitrary attributes, their members are internally represented as a dictionary which isn't necessary when your tens of thousands of objects have a limited set of attributes.
Defining a sequence __slots__ tells the interpreter that your Python class will only need memory for the members defined in that sequence and restricts your ability to arbitrarily add new members (see [1] for details).
[1] https://docs.python.org/2/reference/datamodel.html#slots https://docs.python.org/2/reference/datamodel.html#slots
[2] http://code.activestate.com/lists/python-list/531365/ http://code.activestate.com/lists/python-list/531365/
- TheLoneWolfling 12y agoNote that in PyPy __slots__ is entirely unnecessary: http://morepypy.blogspot.ca/2010/11/efficiently-implementing-python-objects.html http://morepypy.blogspot.ca/2010/11/efficiently-implementing...