Classes and metaclasses for easier ``__slots__`` handling.
Project description
autoslot
Automatic “__slots__”.
Demo
from autoslot import Slots
class Compact(Slots):
def __init__(self, a, b):
self.x = a
self.y = b
This produces exactly the same class as if you had done:
class Compact:
__slots__ = {'x', 'y'}
def __init__(self, a, b):
self.x = a
self.y = b
Simply: the code inside __init__() is scanned to find all assignments to attributes on self, and these are added as __slots__.
The benefit of using autoslot.Slots over a manual slots declaration is that you can modify the code inside the __init__() method to add more attributes, and those changes will automatically be reflected in the __slots__ definition.
You can also have the best of both worlds: slots for fields you expect, as well as a __dict__ for those you don’t:
from autoslot import SlotsPlusDict
class SemiCompact(SlotsPlusDict):
def __init__(self, a, b):
self.x = a
self.y = b
inst = SemiCompact(1, 2)
inst.z = 123 # <-- This won't fail!
Attributes x and y will be stored in slots, while all other dynamically-assigned attributes will go into the usual __dict__ instance inside the class. If most of your class’s attributes appear in the __init__() method (these will become slots), then the space bloat caused by dictionary hash-table expansion will be contained to only the dynamically-assigned attributes.
How does it work?
See for yourself! The code is tiny.
In words: the metaclass finds the __init__() method, if present, and accesses its bytecode. It looks for all assignments to attributes of self, and considers those to be desired __slots__ entries. Then the metaclass injects __slots__ into the namespace of the class definition and thereafter allows class creation to proceed as normal.
Weakref
When __slots__ are used, weak references (e.g. using the weakref standard library module) won’t work. If you need weak references, just set it up on a new __slots__ class variable as you would normally do without using autoslot:
from autoslot import Slots
class Compact(Slots):
__slots__ = ['__weakref__']
def __init__(self, a, b):
self.x = a
self.y = b
Everything else will still work, and instances of Compact will now also play nicely with the weakref module.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file autoslot-2021.10.1.tar.gz
.
File metadata
- Download URL: autoslot-2021.10.1.tar.gz
- Upload date:
- Size: 10.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.25.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f7056c86c3fb58eb8719613a2d403eaf3a9b0f0fe97e202f8c7827bbb8cbeabd |
|
MD5 | 51d96cd883b522495fe025ea322033df |
|
BLAKE2b-256 | ee102a0e8da24812e61ba17bfc138c9e5a0ee6a4492d1651f183a4168adb0cca |
File details
Details for the file autoslot-2021.10.1-py2.py3-none-any.whl
.
File metadata
- Download URL: autoslot-2021.10.1-py2.py3-none-any.whl
- Upload date:
- Size: 7.8 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.25.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8bf9c9a52c1832df159e26c1cbeddd79bfea3ba8d4e69545c67703716c7833c6 |
|
MD5 | b561b2da12ccf45c1f860a73805ee13c |
|
BLAKE2b-256 | 7f73284c5100ed71e78720983354f7c574a21a0a13339d39bb4938e7c321297a |