I used Python 3.5 for this. SOURCE CODE: https://docs.google.com/document/d/1miS5oxCqhxd7jR0frts1gWx7BF9Oq9cdJjtakl5nfmQ/edit?usp=sharing. Objects, values and types¶. Objects are Python’s abstraction for data. All data in a Python program is represented by objects or by relations between objects. (In a sense, and in conformance to Von Neumann’s model of a “stored program computer”, code is also represented by objects.). In Python, there is no default functionality to allocate a static amount of memory while creating the object to store all its attributes. Usage of slots reduce the wastage of space and speed up the program by allocating space for a fixed amount of attributes. Example of python object with slots.
Released:
Decorator to add __slots__ in dataclasses
Project description
Python Slots Machine
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
Monty Python Holy Grail Free
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.
Python __slots__
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
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 |