Skip to main content

No project description provided

Project description

Singleton

About the project

A clean, transparent and easy to use python singleton wrapper.

Installation

pip install better-singleton

Usage

Singleton definition:

from better_singleton import singleton

@singleton
class Counter:
    def __init__(self, initial_counter):
        self.counter = initial_counter

    def inc(self):
        self.counter += 1

Usage:

counter1 = Counter(1)
counter1.inc()
counter2 = Counter(1)
print(counter2.counter)
# >>> 2

Ensures that at most only one instance of the class exists at any point in time. __init__ and __new__ of the original class get called only once: the first time an instance is created. This instance will from then on be the singleton instance. Every following call to the constructor of the wrapped class returns the singleton instance.

The wrapped class cannot have a class variable called _singleton as that stores the singleton instance. This will throw an error before execution begins.

Not thread safe during first initialization.

Roadmap

Contributing

Publish

  1. Bump version in pyproject.toml
  2. poetry update
  3. poetry build
  4. poetry publish

License

See License.md.

Contributors

Stephan Schmiedmayer

Acknowledgments

Team Incentive Mechanism App Cluster of "Advanced Python Programming Spring 2022" Nour Bouzid, Yanjie Li, Stephan Schmiedmayer and Leonhard Zirus under supervision of Valentin Zieglmeier

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

better_singleton-1.0.0.tar.gz (2.6 kB view hashes)

Uploaded Source

Built Distribution

better_singleton-1.0.0-py3-none-any.whl (3.5 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page