Skip to content

more-abc/sconst

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 

Repository files navigation

sconst

Immutable constant types for Python.

Installation

pip install sconst

Usage

Constant types

from sconst import IntConstant, StrConstant, ListConstant, TupleConstant

x = IntConstant(42)
print(x.value)   # 42
x.value = 10     # raises ChangeConstError

s = StrConstant("hello")
print("ell" in s)  # True
s + " world"       # raises ChangeConstError

items = ListConstant([1, 2, 3])
print(items[0])    # 1
for item in items: # iteration works
    print(item)

coords = TupleConstant((10, 20))
print(len(coords)) # 2

const_property descriptor

A read-only property whose value is computed once and then frozen:

from sconst import const_property

class Config:
    @const_property
    def max_retries(self):
        return 5

cfg = Config()
cfg.max_retries        # 5
cfg.max_retries = 10   # raises ChangeConstError
del cfg.max_retries    # raises DeleteConstError

Classes

Class Description
BaseConstant Base class for all constant wrappers
IntConstant Constant integer — blocks arithmetic and mutation
StrConstant Constant string — blocks +, *, and item assignment
IterableConstant Base for iterable constants — supports iteration, indexing, len(), and in
ListConstant Constant list
TupleConstant Constant tuple

Exceptions

Exception Raised when
SconstException Base exception for all sconst errors
ChangeConstError An attempt is made to modify a constant
DeleteConstError An attempt is made to delete a constant attribute

License

MIT

About

Immutable constant types for Python.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages