Python Slots Property
This can be used in exactly the same way as the DataClassCard and NamedTupleCard examples earlier. The attrs project is great and does support some features that data classes do not, including converters and validators. Furthermore, attrs has been around for a while and is supported in Python 2.7 as well as Python 3.4 and up. However, as attrs is not a part of the standard library, it does add. Python老鸟都应该看过那篇非常有吸引力的 Saving 9 GB of RAM with Python’s slots 文章,作者使用了slots让内存占用从25.5GB降到了16.2GB。在当时来说,这相当于用一个非常简单的方式就降低. Slots provide a special mechanism to reduce the size of objects. It is especially useful if you need to allocate thousands of objects that would otherwise take lots of memory space. It is not very common but you may find it useful someday. Using slots (only restricts setting arbitary attributes) Property Descriptor. Python ships with built in function called property. We can use this function to customize the way attributes be accessed and assigned. First I will explain you about property before I get you idea about how it is useful to make attribute readonly. Hashes for dataslots-1.0.2-py2.py3-none-any.whl; Algorithm Hash digest; SHA256: 4fe302ab59c86e01a4fafe516776a198cd8a42dc696dcc9d525e2ec8ee0fe773: Copy.
In Python every class can have instance attributes. By default Pythonuses a dict to store an object’s instance attributes. This is reallyhelpful as it allows setting arbitrary new attributes at runtime.
However, for small classes with known attributes it might be abottleneck. The dict
wastes a lot of RAM. Python can’t just allocatea static amount of memory at object creation to store all theattributes. Therefore it sucks a lot of RAM if you create a lot ofobjects (I am talking in thousands and millions). Still there is a wayto circumvent this issue. It involves the usage of __slots__
totell Python not to use a dict, and only allocate space for a fixed setof attributes. Here is an example with and without __slots__
:
Without__slots__
:
With__slots__
:
The second piece of code will reduce the burden on your RAM. Some peoplehave seen almost 40 to 50% reduction in RAM usage by using thistechnique.
On a sidenote, you might want to give PyPy a try. It does all of theseoptimizations by default.
Below you can see an example showing exact memory usage with and without __slots__
done in IPython thanks to https://github.com/ianozsvald/ipython_memory_usage
Python Slots Property For Sale
Latest versionReleased:
Decorator to add __slots__ in dataclasses
Project description
Decorator for adding slots
Python3.7 provides dataclasses module for faster class creation (PEP 557).Unfortunately there's no support for __slots__. If you want to create more memory efficient instances, you need todo it by yourself or use dataslots.dataslots decorator.
Usage
Simple example
Inheritance
As described in docs, in derived class __dict__ is created, because base class does not have __slots__.Slots are created from all defined properties (returned by dataclasses.fields() function).
Dynamic assignment of new variables
Weakref
Read-only class variables
With __slots__ it's possible to define read-only class variables. When using dataclasses you cannot provide typefor attribute or use typing.ClassVar to declare one.
Pickling frozen dataclass
Because of an issue 36424 you need custom __setstate__ method. In dataslots there isimplemented default version and it is used if decorated class has no __getstate__ and __setstate__ function declared.
More about __slots__
Release historyRelease notifications RSS feed
1.0.2
1.0.2rc2 pre-release
1.0.1
1.0.0
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Filename, size | File type | Python version | Upload date | Hashes |
---|---|---|---|---|
Filename, size dataslots-1.0.2-py2.py3-none-any.whl (4.1 kB) | File type Wheel | Python version py2.py3 | Upload date | Hashes |
Filename, size dataslots-1.0.2.tar.gz (7.5 kB) | File type Source | Python version None | Upload date | Hashes |
Hashes for dataslots-1.0.2-py2.py3-none-any.whl
Python Slot Machine Code
Algorithm | Hash digest |
---|---|
SHA256 | 4fe302ab59c86e01a4fafe516776a198cd8a42dc696dcc9d525e2ec8ee0fe773 |
MD5 | aa8075201eba64938a16361e741a901b |
BLAKE2-256 | b2b22f9f4ea849a076effa673dd9b7e67bedb9358ad0875c30cd4ae0ad6298bc |
Hashes for dataslots-1.0.2.tar.gz
Algorithm | Hash digest |
---|---|
SHA256 | 0dfc4d12aab104b00ddb88a585c0a2227bbb9bd19c785dc8068b43eb0d6009e1 |
MD5 | 656b169564c8623fe9a97aa5f25df7fd |
BLAKE2-256 | a81ca45405ae05d585b786e1819a3406310a097ffd7bf5f104e7c78e63cb86a8 |