Immutable constant types for Python.
pip install sconstfrom 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)) # 2A 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| 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 |
| 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 |