Skip to main content

Simple Python Singleton pattern

PyPI package PyPI version

This module provides a simple way to define a class as a singleton.

Install

You can install this python module via pip:

pip install simple-singleton

Otherwise the module can be downloaded from PyPI: https://pypi.org/project/simple-singleton/

Usage

  1. Import the module:
    from simple_signleton import Singleton
    
    or:
    from simple_signleton import SingletonArgs
    
  2. Create a class that uses one of the above meta classes:
    class NewClass(metaclass=Singleton):
        pass
    
    or:
    class NewClass(metaclass=SingletonArgs):
        pass
    

Difference between Singleton and SingletonArgs

The Singleton class is a very basic implementation of the singleton pattern. All instances of a class are equal. Even if they are initialized with different parameters:

instance1 = SingletonClass(param="value")
instance2 = SingletonClass(param="different_value")

assert instance1 == instance2  # True
print(instance2.param)         # "value"

If you do not want this behavior, use the SingletonArgs meta class. With this class only instances that are initialized with the same parameters are the equal:

instance1 = SingletonArgsClass(param="value")
instance2 = SingletonArgsClass(param="different_value")
instance3 = SingletonArgsClass(param="value")

assert instance1 == instance2  # False
assert instance1 == instance3  # True

print(instance2.param)         # "different_value"

Usage in multi-threaded environments

The Singleton and SingletonArgs meta classes are not thread-safe!

To use them in a multi-threaded environment, please use the

  • ThreadSingleton and
  • ThreadSingletonArgs

meta classes. They can be used exactly like the standard meta classes.

Metadata

Release files for simple-singleton 2.0.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 simple-singleton 2.0.0
File Size Uploaded
simple_singleton-2.0.0.tar.gz 15.1 kB Details

Built distribution (wheel)

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

Total release size: 31.8 kB

Release files / simple_singleton-2.0.0.tar.gz

Download URL simple_singleton-2.0.0.tar.gz
Size 15.1 kB
Tags Source
SHA-256 checksum
How to use checksums
5f5a1292305940c15e6eaf837cff68e81b19142dd07a0868c93cb4c492ef22ad
BLAKE2b-256 checksum
How to use checksums
1d767e70d62ce4af2f89f557406b356589ca3509253dcbbfdb8f7292808d6e0f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.7.17 Linux/6.2.0-1012-azure

Release files / simple_singleton-2.0.0-py3-none-any.whl

Download URL simple_singleton-2.0.0-py3-none-any.whl
Size 16.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
831c4b082ecd37091a86eba370dc9788601c41bf15c896b07a9a242c8b008f43
BLAKE2b-256 checksum
How to use checksums
4c2bfb243f5ee328ec582713cf2399adda4db44199e9e62525ba63afcb64e112
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.7.17 Linux/6.2.0-1012-azure

Release history Release notifications | RSS feed

This release

2.0.0 This release

2 release files

1.5.1

2 release files

1.5

2 release files

1.4

2 release files

1.3.1

2 release files

1.3

2 release files

1.2.1

2 release files

1.1

2 release files

1.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