Skip to main content
https://img.shields.io/badge/stdlib--only-yes-green.svg https://coveralls.io/repos/github/cjrh/autoslot/badge.svg?branch=master https://img.shields.io/pypi/pyversions/autoslot.svg https://img.shields.io/github/tag/cjrh/autoslot.svg https://img.shields.io/badge/install-pip%20install%20autoslot-ff69b4.svg https://img.shields.io/pypi/v/autoslot.svg https://img.shields.io/badge/calver-YYYY.MM.MINOR-22bfda.svg

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.

Developer Setup

This project uses nox with the uv backend for running tests across multiple Python versions.

# Create a virtual environment
uv venv --python=3.13 .venv
source .venv/bin/activate

# Install nox with uv support
uv tool install 'nox[uv]'

# Run tests on all Python versions (auto-downloads missing interpreters)
nox --download-python=always

# Run tests on a specific version
nox -s tests-3.14

# List available test sessions
nox -l

Release files for autoslot 2025.11.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for autoslot 2025.11.1
File Size Uploaded
autoslot-2025.11.1.tar.gz 11.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for autoslot 2025.11.1
File Interpreter ABI Platform
autoslot-2025.11.1-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 19.6 kB

Release files / autoslot-2025.11.1.tar.gz

Download URL autoslot-2025.11.1.tar.gz
Size 11.5 kB
Tags Source
SHA-256 checksum
How to use checksums
6ccaf4aa6db21e8a4b04a97338fb9efd03c4544cf1c9ac5d0fb12d70a920cb08
BLAKE2b-256 checksum
How to use checksums
3cc8818ff7ed59421f965cc2284ad20686da17e917d0c6d687c2d1b045555527
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.32.5

Release files / autoslot-2025.11.1-py2.py3-none-any.whl

Download URL autoslot-2025.11.1-py2.py3-none-any.whl
Size 8.1 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
fa642b58b082d0021177fcef11dd0634a3fa3e8f078828cade52bc9a44c337f5
BLAKE2b-256 checksum
How to use checksums
bdf3376b4977beeafca00bd9b0cd84e1277c093fe3aac65507054fe025be55c3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.32.5

Release history Release notifications | RSS feed

This release

2025.11.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page