Skip to main content

Classes and metaclasses for easier ``__slots__`` handling.

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

autoslot-2021.4.2.tar.gz (10.6 kB view details)

Uploaded Source

Built Distribution

autoslot-2021.4.2-py2.py3-none-any.whl (7.8 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file autoslot-2021.4.2.tar.gz.

File metadata

  • Download URL: autoslot-2021.4.2.tar.gz
  • Upload date:
  • Size: 10.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.25.1

File hashes

Hashes for autoslot-2021.4.2.tar.gz
Algorithm Hash digest
SHA256 80304e67a64335aefe4bd8e033a12e874ff6cc05e1c5461a905f3b77888abe55
MD5 4c8cd0ffe5de24b880c60b7cb5551740
BLAKE2b-256 125cdad423b042beecc71313bbb0b6dd6d8725027239a828a15b752024d51dc6

See more details on using hashes here.

File details

Details for the file autoslot-2021.4.2-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for autoslot-2021.4.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 2725dc5bebe577f157ee251300280e71f6e8a0622bc6eef871dad32fe8d12153
MD5 ad89cea0926c9b0f5f5d7f819e336cd6
BLAKE2b-256 765a1f5c5ead8109370c8c3ab7741225515ddbdc0ce3e0a3be4b73b09f9f694a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page