Skip to main content

Any-singleton

Provides decorators and utilities for implementing the singleton pattern.

GitHub Repo stars PyPI - License PyPI - Version PyPI - Python Version

Installation

python -m pip install any-singleton

Documentation

Usage

Create a singleton

Register a singleton with a value:

from any_singleton import singleton

tea = singleton('my_project.main.coffee', 'tea')

Instantiate an object and register as a singleton:

from any_singleton import singleton

my_range = singleton('my_project.main.coffee', range, 123)

For disambiguating, you can use singleton_value() to instead singleton() when the value is a type:

from any_singleton import singleton_value

class Tea:
    pass

tea = singleton_value('my_project.main.coffee', type(Tea))

Make a function can only be called once in global

Using @once to create a function that can only be called once in global.

import tomllib
from any_singleton import once, singleton

@once('my_project.initializations.init')
def init(config_path: str) -> None:
    with open(config_path, 'rb') as f:
        config = singleton('my_project.globals.config', tomllib.load(f))

init('config.toml')

Or just using @run_once to create a function as same as decorated with @once and calling it immediately.

import tomllib
from any_singleton import run_once, singleton

@run_once('my_project.initializations.init', 'config.toml')
def init(config_path: str) -> None:
    with open(config_path, 'rb') as f:
        config = singleton('my_project.globals.config', tomllib.load(f))

ATTENTION

  • any-singleton will OCCUPY the global variable _any_singleton, see any_singleton.singletons.GLOBAL_KEY.
  • DO NOT use *.@cached_return and any_singleton.nonreferenced_types as a domain name. They're RESERVED words.

View this source code on GitHub.

Release files for any-singleton 1.1.0

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

Source distribution (sdist)

Source distribution for any-singleton 1.1.0
File Size Uploaded
any_singleton-1.1.0.tar.gz 4.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for any-singleton 1.1.0
File Interpreter ABI Platform
any_singleton-1.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 10.5 kB

Release files / any_singleton-1.1.0.tar.gz

Download URL any_singleton-1.1.0.tar.gz
Size 4.6 kB
Tags Source
SHA-256 checksum
How to use checksums
fd4b8352a2ec49e9b193257112e44f842c42a546dc2cb800b094b7e34df95528
BLAKE2b-256 checksum
How to use checksums
e0b106590377e8d614c4cfcb8039aa8433d7b1fc8d69f4f4cb3a9fb46f4db4e0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 10, 2026.

Transparency log

Release files / any_singleton-1.1.0-py3-none-any.whl

Download URL any_singleton-1.1.0-py3-none-any.whl
Size 5.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
47ce19c60c9c2ad3c1da4b55d7714fea99fbae1ddd7a2b95c6e499ff7971ae37
BLAKE2b-256 checksum
How to use checksums
aeef9b73e4d92554d2cb9a0f10ee4364f0506f5e7d297663012a39c10458e7cc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 10, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

1.1.0 This release

2 release files

1.0.1

2 release files

1.0.0

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