Skip to main content

Class decorator to enable @cached_property on classes with __slots__

Project description

reslot

A class decorator that lets you use @cached_property on classes with __slots__.

The decorated class needs a __dict__ slot, but there won't exactly be a dictionary in it. Instead, @reslot will give it a custom MutableMapping with its own slots. @cached_property will keep its cache there.

The custom MutableMapping's slots will be exactly the slots needed to store the results of every @cached_property that was on the class at the time it was declared. This minimizes the memory footprint and makes lookup speedy.

Usage

from functools import cached_property

from reslot import reslot

BEANED = False

@reslot
class Spam:
    __slots__ = ("__dict__",)

    @cached_property
    def eggs(self):
        return {"delicious": False}

    @cached_property
    def beans(self):
        global BEANED
        BEANED = True
        return {"spam": 111000, "delicious": True}

spam = Spam()
print(spam.eggs)   # {"delicious": False}
print(BEANED)      # False
print(spam.beans)  # {"spam": 111000, "delicious": True}
print(BEANED)      # True

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

reslot-0.10.tar.gz (3.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

reslot-0.10-py3-none-any.whl (3.9 kB view details)

Uploaded Python 3

File details

Details for the file reslot-0.10.tar.gz.

File metadata

  • Download URL: reslot-0.10.tar.gz
  • Upload date:
  • Size: 3.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.3

File hashes

Hashes for reslot-0.10.tar.gz
Algorithm Hash digest
SHA256 27d3dda00327cdaf72a1d045d39e46eec6324be0c9a219e73c8e69df9102037d
MD5 7a09665f650dfb6388422f995d5ce748
BLAKE2b-256 be8a130f01a603f95ca7c39ecf0fb9515154de159a42cc6641e76bd469f9ec77

See more details on using hashes here.

File details

Details for the file reslot-0.10-py3-none-any.whl.

File metadata

  • Download URL: reslot-0.10-py3-none-any.whl
  • Upload date:
  • Size: 3.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.3

File hashes

Hashes for reslot-0.10-py3-none-any.whl
Algorithm Hash digest
SHA256 2749039d152f74cb52eba4b662aad334a05a4811a7ba9831d2f72c99e69c2012
MD5 2cfece1a5f8b102e3c1ffa4eb12da459
BLAKE2b-256 071c9fb09787fdd0a7d2ec5d1299014f638d2be1ef876bc5cf4cc3be29800630

See more details on using hashes here.

Supported by

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