singleton-base
A type-friendly, thread-safe singleton base class for Python 3.12+. Very simple to use and test-friendly.
Installation
Using pip
pip install singleton-base
Using Uv
uv add singleton-base
Using Poetry
poetry add singleton-base
Features
- Thread-safe: Multiple threads can safely create instances
- Type-friendly: Full type hint support, including
Selfreturns and PEP 695 generic syntax - Test-friendly: Easy instance management for testing scenarios
- Zero runtime dependencies
Usage
Usage Example
Subclass using SingletonBase to make your class a singleton. All returned instances will now be the same object.
from singleton_base import SingletonBase
class MySingleton(SingletonBase):
"""A singleton class example that holds an integer value."""
def __init__(self, value: int) -> None:
self.value = value
class AnotherSingleton(SingletonBase):
"""Another singleton class example that holds a string value."""
def __init__(self, value: str) -> None:
self.value = value
# Initially, no instance exists
print(MySingleton.has_instance()) # False
# Two equivalent ways to create the singleton:
instance = MySingleton(42)
# or
instance = MySingleton.get_instance(value=42)
print(MySingleton.has_instance()) # True
print(instance.value) # 42
# Subsequent calls return the same instance.
# Note: any args passed after the first construction are ignored.
instance2 = MySingleton.get_instance(value=99)
print(instance is instance2) # True
print(instance2.value) # 42
# Other singleton classes are independent — each maintains its own instance:
another = AnotherSingleton(value="Hello")
print(instance is another) # False
# Reset to allow re-initialization (handy for tests):
MySingleton.reset_instance()
print(MySingleton.has_instance()) # False
# After a reset, the next call constructs a fresh instance:
instance3 = MySingleton.get_instance(value=9001)
print(instance3.value) # 9001
Available Methods
| Method | Description |
|---|---|
get_instance(*args, **kwargs) |
Returns the singleton instance, constructing it with *args, **kwargs if it does not yet exist. |
has_instance() |
Returns True if the singleton instance has been created. |
reset_instance() |
Destroys the current instance, allowing re-initialization. |
Wrapper API: SingletonWrap
When subclassing isn't an option (third-party classes, dataclasses, etc.), use SingletonWrap to wrap an existing class instead:
from singleton_base import SingletonWrap
class Service:
def __init__(self, host: str, port: int) -> None:
self.host = host
self.port = port
service_singleton = SingletonWrap(Service, "localhost", 8080)
service = service_singleton.get() # constructs on first access
same_service = service_singleton.get() # returns the same instance
print(service is same_service) # True
service_singleton.reset_instance()
print(service_singleton.has_instance()) # False
SingletonWrap exposes the same has_instance() / reset_instance() lifecycle as SingletonBase, plus get() / get_instance() (aliases) for retrieval.
Python Version Compatibility
Supports Python 3.12–3.14 with full test coverage across every version. No version-conditional code, no runtime dependencies.
Metadata
Release files for singleton-base 1.2.10
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| singleton_base-1.2.10-py3-none-any.whl | Python 3 | none | any | Details |
Release files / singleton_base-1.2.10-py3-none-any.whl
| Download URL | singleton_base-1.2.10-py3-none-any.whl |
|---|---|
| Size | 12.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7d5ea7ec590aabe135003879abb6d5742fe92f74cfa3ceb4e8aac016672291a1
|
|
BLAKE2b-256 checksum How to use checksums |
78c8078394306098e390657570d0a11027dbd4f838f399260987d82be3ec0d5d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Oct 9, 2026.
Transparency log