Skip to main content

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).

Metadata

Release files for statics 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for statics 1.0.0
File Size Uploaded
statics-1.0.0.tar.gz 2.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for statics 1.0.0
File Interpreter ABI Platform
statics-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 5.3 kB

Release files / statics-1.0.0.tar.gz

Download URL statics-1.0.0.tar.gz
Size 2.4 kB
Tags Source
SHA-256 checksum
How to use checksums
908a2d1ed3eef907f38b280d367fd0f5370d7b19f5088c0de886501e256bd4da
BLAKE2b-256 checksum
How to use checksums
b765349ce4596f2d6368312aaf539a41e8278a8311a1d3181708dc40b156735d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/4.0.1 CPython/3.11.5

Release files / statics-1.0.0-py3-none-any.whl

Download URL statics-1.0.0-py3-none-any.whl
Size 2.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ffe4c3a3f5006e8a1d9c47716bb3f4f4acff20b5b7787dd9c96110e357361158
BLAKE2b-256 checksum
How to use checksums
d9307032a023114b6d5452198cf1ad460e140c5f8ec52740b66059bf19413c4f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/4.0.1 CPython/3.11.5

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page