2 ms·
>That being said __slots__ as a performance enhancement is crazy useful Can you be more specific? The article claims that it's not a performance boost at all.
by Dylan16807 12y ago
>That being said __slots__ as a performance enhancement is crazy useful
Can you be more specific? The article claims that it's not a performance boost at all. Where is it useful?
- ejstronge 12y agoThe 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...