Skip to main content

Decorator for implementing singleton classes

Project description

Decorator for implementing singleton pattern

Usage

To install, add pygleton module to your project. Then add from pygleton import singleton to your file.

There are two ways to use the decorator: without repetetive __init__() calls and with them (you do not want this unless you know what are you doing).

Main scenario: call init only the first time

Do not pass any parameter to the decorator:

@singleton()
class Foo:
    ...

Now, when you do Foo(...) for the first time, object instance will be created, and Foo init method would be called. Any subsequent Foo(...) calls will return the same instance, and Foo.__init__ will be completely ignored.

I know what I am doing: I want to call init every time

Pass truthy value to recall_init parameter of the decorator:

@singleton(recall_init=True)
class Foo:
    ...

Now, when you do Foo(...) for the first time, object instance will be created, and all subsequent Foo(...) calls will return the same instance. However, init method will be called every time.

Usage example

See test file.

Development

The project uses poetry (highly recommended) and you should know your way around. The supplied Makefiles allows to run linters, check formatting, and run tests (100% coverage is required!) simply by typing make. Everything should pass.

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

pygleton-1.0.1.tar.gz (3.0 kB view hashes)

Uploaded Source

Built Distribution

pygleton-1.0.1-py3-none-any.whl (3.4 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