Skip to main content

Create const variables everywhere

Project description

consts-everywhere

Create const variables everywhere. This is not just a __setattr__ trick that only makes it possible to create const members. You can also create local and global consts.

Installation

pip install consts-everywhere

How To Use

We have a proxy class:

class Const(wrapped)

This class inherits a variant of ObjectProxy from wrapt. Along with all augmented assignment operators, this class also overloads the normal assignment operator (=) thanks to assign-overload. wrapped can be any object and the resulting instance of this class will act like wrapped in every way. If you use this class, you need to call consts_everywhere.patch_and_reload_module(). This function only exposed in this package as a convenience, actually it is the exact copy of assign_overload.patch_and_reload_module() and you can find documentation about it in assign-overload. Usage example:

import consts_everywhere

def main():
    a = consts_everywhere.Const(10)
    print(a) # prints 10
    a = 20 # Error

if consts_everywhere.patch_and_reload_module():
    main()

One thing to note is if you assign a variable holding a Const instance to a new variable, new variable takes the value of the Const instance, not the value of its underlying object. However, most operators return the underlying object. Example:

import consts_everywhere

def main():
    a = consts_everywhere.Const(10)
    print(type(a+5)) # prints int
    b = a
    print(type(b)) # prints Const

if consts_everywhere.patch_and_reload_module():
    main()

If you want to access the underlying object, use the __wrapped__ attribute:

import consts_everywhere

def main():
    a = consts_everywhere.Const(10)
    b = a.__wrapped__
    print(type(b)) # prints int

if consts_everywhere.patch_and_reload_module():
    main()

Const is also compatible with typeguard. Function argument type enforcement example:

import typeguard
import consts_everywhere

@typeguard.typechecked
def test4(a: consts_everywhere.Const[int]):
    pass

def main():
    a = consts_everywhere.Const(10)
    b = consts_everywhere.Const("abc")
    test4(a) # Ok
    test4(b) # Error

if consts_everywhere.patch_and_reload_module():
    main()

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

consts_everywhere-1.0.1.tar.gz (37.3 kB view details)

Uploaded Source

Built Distribution

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

consts_everywhere-1.0.1-py2.py3-none-any.whl (43.1 kB view details)

Uploaded Python 2Python 3

File details

Details for the file consts_everywhere-1.0.1.tar.gz.

File metadata

  • Download URL: consts_everywhere-1.0.1.tar.gz
  • Upload date:
  • Size: 37.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.0

File hashes

Hashes for consts_everywhere-1.0.1.tar.gz
Algorithm Hash digest
SHA256 18ed231503008a829374eeb66b4ae4106ef7f6487fdaaa2ca9d1959f66df8808
MD5 a3043d64d7a4be721ba63a5eaf62c670
BLAKE2b-256 2118a0e45e7a7d8355ad8f1e1f8955c5339eb75e78ca6ed5d6ef961f4cc4d420

See more details on using hashes here.

File details

Details for the file consts_everywhere-1.0.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for consts_everywhere-1.0.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 3515d81f5643eb13e3774ed4451ecd84c4370d114871c0280350201428d717a7
MD5 d2904aee1e5de6c6701e7a083b9a9096
BLAKE2b-256 9e58192795425d7d00792d5b7852ec25657d8a76a9263be2af6df345b0c1e8ff

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