What is the purpose of __slots__
in Python — especially with respect to when I would want to use it, and when not?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
Slots are very useful for library calls to eliminate the "named method dispatch" when making function calls. This is mentioned in the SWIG documentation. For high performance libraries that want to reduce function overhead for commonly called functions using slots is much faster.
Now this may not be directly related to the OPs question. It is related more to building extensions than it does to using the slots syntax on an object. But it does help complete the picture for the usage of slots and some of the reasoning behind them.
Quoting Jacob Hallen:
Each python object has a
__dict__
atttribute which is a dictionary containing all other attributes. e.g. when you typeself.attr
python is actually doingself.__dict__['attr']
. As you can imagine using a dictionary to store attribute takes some extra space & time for accessing it.However, when you use
__slots__
, any object created for that class won't have a__dict__
attribute. Instead, all attribute access is done directly via pointers.So if want a C style structure rather than a full fledged class you can use
__slots__
for compacting size of the objects & reducing attribute access time. A good example is a Point class containing attributes x & y. If you are going to have a lot of points, you can try using__slots__
in order to conserve some memory.In addition to the other answers, here is an example of using
__slots__
:So, to implement
__slots__
, it only takes an extra line (and making your class a new-style class if it isn't already). This way you can reduce the memory footprint of those classes 5-fold, at the expense of having to write custom pickle code, if and when that becomes necessary.The original question was about general use cases not only about memory. So it should be mentioned here that you also get better performance when instantiating large amounts of objects - interesting e.g. when parsing large documents into objects or from a database.
Here is a comparison of creating object trees with a million entries, using slots and without slots. As a reference also the performance when using plain dicts for the trees (Py2.7.10 on OSX):
Test classes (ident, appart from slots):
testcode, verbose mode:
Another somewhat obscure use of
__slots__
is to add attributes to an object proxy from the ProxyTypes package, formerly part of the PEAK project. ItsObjectWrapper
allows you to proxy another object, but intercept all interactions with the proxied object. It is not very commonly used (and no Python 3 support), but we have used it to implement a thread-safe blocking wrapper around an async implementation based on tornado that bounces all access to the proxied object through the ioloop, using thread-safeconcurrent.Future
objects to synchronise and return results.By default any attribute access to the proxy object will give you the result from the proxied object. If you need to add an attribute on the proxy object,
__slots__
can be used.