Skip to main content

###Singleton Decorator Library

A decorator based on wiki.python.org Python Decorators Library, namely https://wiki.python.org/moin/PythonDecoratorLibrary#Singleton, with significant improvements.

Features (assuming you decorate a class definition of MyClass)

  • Every call of MyClass() results in the same instance, which persists from the first instantiation until the end of execution
  • Even if you delete all your references to the instance, it still persists to be returned on the next call of MyClass()
  • That instance is only initiallized once (the first time)
  • is_instance(MyClass(), MyClass) is True (that is, we do not wrap the class)
  • Using copy() or deepcopy() simply gives another reference to the single instance
  • The overhead of locking in the threaded version is low enough that you may use it even before adding threading to your class.
  • If MyClass is threadsafe, then annotating with @singleton will give a threadsafe singleton (only the safety of creating the first instance requires locking, as all other singleton actions are no-ops or simply return a reference to the single instance)
  • If MyClass has locking (either Lock or RLock) in the initialization (new and init) the singleton locking does not deadlock.
  • The test suite is included in the src package, so you can tell if I verified correctly
  • The singleton decorator and test bench pass black and mypy
  • The test suite is run before publishing

This has some fixes (correct calling of old init, correct replacement of init, removal of copy and deepcopy, and threadsafety) from the version on the PythonDecoratorLibrary wiki page.

from singleton_decorator1 import singleton

@singleton
class MyClass
    pass

a = MyClass()
b = MyClass()
assert(a is b)
c = deepcopy(b)
assert(a is c)

Note that if MyClass is not threadsafe, then the decorator will not fix that -- it only ensures that the singleton functionality is thread safe.

The library can be built with hatchling:

repository issues PyPI

Metadata

Release files for singleton-decorator1 1.0.2

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

Source distribution (sdist)

Source distribution for singleton-decorator1 1.0.2
File Size Uploaded
singleton_decorator1-1.0.2.tar.gz 4.2 MB Details

Built distribution (wheel)

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

Total release size: 4.2 MB

Release files / singleton_decorator1-1.0.2.tar.gz

Download URL singleton_decorator1-1.0.2.tar.gz
Size 4.2 MB
Tags Source
SHA-256 checksum
How to use checksums
0ff46bc7a33f837aac9bbe57305938d6a772bfdc4d89232087061d04df98e4fc
BLAKE2b-256 checksum
How to use checksums
2484a56826013359a1ec75d1b044258e6edd1227b52756fa0188f0c207285cb7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.6

Release files / singleton_decorator1-1.0.2-py3-none-any.whl

Download URL singleton_decorator1-1.0.2-py3-none-any.whl
Size 17.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
35d713a3ccaa943f138e8868b5ba5a7133b5928cebbb8da4686b9bf8b3c5dfd3
BLAKE2b-256 checksum
How to use checksums
262b36679087ef1a177db9291dd8c223137dcb2c59e4c99c05dc31a342d96797
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.6

Release history Release notifications | RSS feed

This release

1.0.2 This release

2 release files

1.0.0

2 release files

0.5.6

2 release files

0.5.5

2 release files

0.5.2

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.3.2

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