lazy-static
lazy module constants -- think lazy imports but for assignments
roughly named after the (deprecated) rust crate lazy_static
installation
pip install lazy-static
why ?
python 3.15 adds lazy imports which are neat but are a little too easy to accidentally make eager.
take this small example for a little wrapper around pyyaml:
import functools
lazy import yaml
load = functools.partial(yaml.load, Loader=yaml.CSafeLoader)
did you catch the bug? yeah that import isn't lazy due to accessing attributes!
it would be great if I could do:
lazy load = functools.partial(yaml.load, Loader=yaml.CSafeLoader)
but there's no such thing...1 unless...
Usage
lazy-static adds a single api: @lazy_static.lazy
taking a simple example:
import argparse
from lazy_static import lazy
@lazy
def computed_once() -> int:
print('computing!')
return 5 * 5 * 5
def main() -> int:
parser = argparse.ArgumentParser()
parser.parse_args() # just for `--help`
print(f'got {computed_once}')
return 0
if __name__ == '__main__':
raise SystemExit(main())
the lazy attribute is deferred when not accessed:
$ python3 t.py --help
usage: t.py [-h]
options:
-h, --help show this help message and exit
but acts like a normal variable otherwise!
$ python3 t.py
computing!
got 125
it even works with static typing!
$ diff -u t.py.bak t.py
--- t.py.bak 2026-07-24 17:47:14.278681851 -0400
+++ t.py 2026-07-24 17:47:22.759961815 -0400
@@ -10,6 +10,7 @@
parser = argparse.ArgumentParser()
parser.parse_args() # just for `--help`
print(f'got {computed_once}')
+ reveal_type(computed_once)
return 0
if __name__ == '__main__':
$ mypy t.py
t.py:13: note: Revealed type is "int"
Success: no issues found in 1 source file
if we take the earlier example we can define our load partial lazily while
preserving the lazy import!
import functools
from lazy_static import lazy
lazy import yaml
@lazy
def load():
return functools.partial(yaml.load, Loader=yaml.CSafeLoader)
you can also use lazy with a lambda if you don't want a full function:
import functools
from lazy_static import lazy
lazy import yaml
load = lazy(lambda: functools.partial(yaml.load, Loader=yaml.CSafeLoader))
-
yes yes you can do stuff with
@functools.cachebut that adds an ugly function call! ↩
Metadata
Release files for lazy-static 1.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| lazy_static-1.1.1.tar.gz | 3.4 kB | Without provenance | Details for lazy_static-1.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| lazy_static-1.1.1-py3-none-any.whl | Python 3 | none | any | Details for lazy_static-1.1.1-py3-none-any.whl |
Total release size: 7.1 kB
Release files / lazy_static-1.1.1.tar.gz
| Download URL | lazy_static-1.1.1.tar.gz |
|---|---|
| Size | 3.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3043b2b6ed51caab7a79a2a6e1af22b8587fb8407fa87d6e90a6642a99e01617
|
|
BLAKE2b-256 checksum How to use checksums |
2055e400e54db0d5486e7e7742268b2ccc1fbad16c608913ae6e647ff369a8e1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.4
|
Release files / lazy_static-1.1.1-py3-none-any.whl
| Download URL | lazy_static-1.1.1-py3-none-any.whl |
|---|---|
| Size | 3.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
178f7246731812340d23fad606ca7a9f50b1c324090406da82911e7cb73e2aaf
|
|
BLAKE2b-256 checksum How to use checksums |
aeedfc406a96a0133b48208c27cbc036dd61c89b4ddb2b94f476916d09305db6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.4
|
Security for lazy-static 1.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.