Static objects in Python functions.
Project description
statics
Static objects in Python functions
This is a micropackage with a decorator @static
that can be used to achieve
static objects to Python functions. The objects can then be loaded into
variables in a function, exactly as they were given, without using globals or
closures.
How this works is probably easier shown than explained:
>>> from statics import static
>>>
>>> x = 1
>>>
>>> @static(x)
... def f():
... x = "__static__"
... print(f"{x=}")
...
>>> del x
>>>
>>> f()
x=1
Multiple static objects can be declared and assigned as a tuple:
>>> x, y, z = 1, 2, 3
>>>
>>> @static(x, y, z)
... def f():
... x, y, z = "__static__"
... print(f"{x=}, {y=}, {z=}")
...
>>> del x, y, z
>>>
>>> f()
x=1, y=2, z=3
Static names in the function do not have to match the names outside the function:
>>> x, y, z = 1, 2, 3
>>>
>>> @static(x, y, z)
... def f():
... a, b, c = "__static__"
... print(f"{a=}, {b=}, {c=}")
...
>>> del x, y, z
>>>
>>> f()
a=1, b=2, c=3
Technically, the static binding is achieved by storing the objects in the
co_const
array of a Python function's code object. Loading the variables
works by replacing the string literal "__static__"
with the static objects.
The string "__static__"
can therefore not be used for anything else in the
decorated function (but strings containing "__static__"
as a substring are
not affected).
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file statics-1.0.0.tar.gz
.
File metadata
- Download URL: statics-1.0.0.tar.gz
- Upload date:
- Size: 2.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.1 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 908a2d1ed3eef907f38b280d367fd0f5370d7b19f5088c0de886501e256bd4da |
|
MD5 | 786fe223f05fad7e405bca5e561dff17 |
|
BLAKE2b-256 | b765349ce4596f2d6368312aaf539a41e8278a8311a1d3181708dc40b156735d |
File details
Details for the file statics-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: statics-1.0.0-py3-none-any.whl
- Upload date:
- Size: 2.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.1 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ffe4c3a3f5006e8a1d9c47716bb3f4f4acff20b5b7787dd9c96110e357361158 |
|
MD5 | 4547b1ade53c5a0f2f453e6ff99dc157 |
|
BLAKE2b-256 | d9307032a023114b6d5452198cf1ad460e140c5f8ec52740b66059bf19413c4f |