A type-friendly, thread-safe singleton base class for Python 3.9+. Very simple to use and test-friendly.
Project description
singleton-base
A type-friendly, thread-safe singleton base class for Python 3.9+. 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 with modern typing on Python 3.11+
- Version adaptive: Automatically uses legacy typing on Python < 3.11 to ensure compatibility
- Test-friendly: Easy instance management for testing scenarios
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):
self.value = value
class AnotherSingleton(SingletonBase):
"""Another singleton class example that holds a string value."""
def __init__(self, value: str):
self.value = value
# Check if instance exists (initially False)
print(f"Instance Exists: {MySingleton.has_instance()}") # False
# Two ways to create the singleton (both do the same thing):
instance = MySingleton(42)
# Safe pattern - always use init=True when instance might not exist
instance: MySingleton = MySingleton.get_instance(init=True, value=42)
print(f"Instance Exists: {MySingleton.has_instance()}, Value: {instance.value}") # True
instance2: MySingleton = MySingleton.get_instance()
print(f"Same instance: {instance is instance2}") # True
# Other singleton classes can be created independently
another_instance: AnotherSingleton = AnotherSingleton.get_instance(init=True, value="Hello")
print(f"Same instance: {instance2 is another_instance}") # False
# This will raise RuntimeError: calling get_instance() without init=True when no instance exists:
try:
MySingleton.reset_instance()
instance = MySingleton.get_instance() # RuntimeError
except RuntimeError as e:
print(e) # Instance of MySingleton is not initialized yet
print(f"Instance Exists: {MySingleton.has_instance()}") # False
instance = MySingleton.get_instance(init=True, value=69)
# an alternative way to do this is to check using has_instance
if MySingleton.has_instance():
instance = MySingleton.get_instance()
print(f"Instance Already Exists: {MySingleton.has_instance()}, Value: {instance.value}") # 69
else:
instance = MySingleton.get_instance(init=True, value=9001)
print(f"Instance Created: {MySingleton.has_instance()}, Value: {instance.value}") # Won't be printed
Available Methods
| Method | Description |
|---|---|
| get_instance(init=False, **kwargs) | Returns singleton instance. If init=True, creates it with kwargs |
| has_instance() | Returns True if singleton instance exists |
| reset_instance() | Destroys current instance, allows creating a new one |
Python Version Compatibility
Python 3.11+ uses modern implementation with more modern type hints. Python 3.9-3.10 automatically falls back to legacy implementation. Full test coverage across all supported versions.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file singleton_base-1.2.5-py3-none-any.whl.
File metadata
- Download URL: singleton_base-1.2.5-py3-none-any.whl
- Upload date:
- Size: 6.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.28 {"installer":{"name":"uv","version":"0.9.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
72e62b1fd66ae8b2314919b53c34ac525885d3459bf7f1803f90ebe5f7fc8683
|
|
| MD5 |
dd5d04e1e240e28d43294a511535e3df
|
|
| BLAKE2b-256 |
b721dccab1473d49863db6a97088608f05f0b955edbbb8d018cbbe9e7b8667c4
|