New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

simple-singleton

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

simple-singleton

Simple way to create a singleton class.

  • 2.0.0
  • PyPI
  • Socket score

Maintainers
1

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.

FAQs


Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc