Skip to main content

Simple singleton class and function decorators with 0 overhead

Project description

Pinstance

Simple singleton class decorator with ability to call bounded methods with 0 overhead.

Decorated class can inherit other classes and can be inherited.

Decorated class can not be collected till the end of a programm

Usage

Decorate a class:

@instanceclass
class MyClass:
    def __init__(self, val):
        self.class_var = val

Now you can instanciate it

inst = MyClass(777)
inst.class_var  ## 777

and access the instance anywhere via get_instance() class method:

MyClass.get_instance().class_var  ## 777
MyClass.get_instance().some_var = 555
MyClass.get_instance().class_var  ## 555

Furthermore, to simplify calling methods via instance reference one can decorate desiered @instanceclass methods:

@instancemethod
def my_function(self, param):
    print(f'{self.class_var} | {param}')

and call them like static methods with 0 overhead:

MyClass.my_function('example')  ## 555 | example

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

pinstance-0.1.0.tar.gz (2.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pinstance-0.1.0-py3-none-any.whl (2.9 kB view details)

Uploaded Python 3

File details

Details for the file pinstance-0.1.0.tar.gz.

File metadata

  • Download URL: pinstance-0.1.0.tar.gz
  • Upload date:
  • Size: 2.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.13

File hashes

Hashes for pinstance-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d60146d42ed711cd87ab499dbcb5e2cbc88d1e5fe7f80ad9cdf9dfacd4380b28
MD5 e14998d7de5053f01e65d7309bb25c11
BLAKE2b-256 48c4e5d7b1278b86d897e34c444e89e9e964b4b5dde527a460afb3b658616b6a

See more details on using hashes here.

File details

Details for the file pinstance-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: pinstance-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 2.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.13

File hashes

Hashes for pinstance-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 88cc0979c08350adda77d98fee0a80faded8cdb264679be36ece92a964f6d952
MD5 0a413f6ba3360b59695cc0cc0bafbdff
BLAKE2b-256 7dba540271a11ca0d52590230d4672b3e2fd3e4e02879f06fde99e250dd3c2ad

See more details on using hashes here.

Supported by

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