Simple way to create a singleton class.
Project description
Simple Python Singleton pattern
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
- Import the module:
from simple_signleton import Singleton
or:from simple_signleton import SingletonArgs
- 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"
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
simple-singleton-1.0.tar.gz
(3.2 kB
view hashes)
Built Distribution
Close
Hashes for simple_singleton-1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0f8887fd15a1af44abdeab0a8eef57d7fd8c09b247e53ba32e1874350d0e4546 |
|
MD5 | c898829cb6b1d328328cf8fabf53eb32 |
|
BLAKE2b-256 | a06fbff6b25dc4a0c9f4f772878ec1f50672e93d3f5d42ab7a35028845ab08a4 |