Skip to main content

build status pre-commit.ci status

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))
  1. yes yes you can do stuff with @functools.cache but that adds an ugly function call! ↩

Metadata

Release files for lazy-static 1.1.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 lazy-static 1.1.0
File Size Uploaded Provenance Actions
lazy_static-1.1.0.tar.gz 3.4 kB Without provenance Details for lazy_static-1.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for lazy-static 1.1.0
File Interpreter ABI Platform Actions
lazy_static-1.1.0-py3-none-any.whl Python 3 none any Details for lazy_static-1.1.0-py3-none-any.whl

Total release size: 7.1 kB

Release files / lazy_static-1.1.0.tar.gz

Download URL lazy_static-1.1.0.tar.gz
Size 3.4 kB
Tags Source
SHA-256 checksum
How to use checksums
248c8f99a7e061493ae57bbec07d6e47cca7ad005858a3ab69ff7aafd16b73f3
BLAKE2b-256 checksum
How to use checksums
0dc342f7278ea924f8dea6ddf25cf0ba9ba4fb8f0d1e89c3b432e7f5c238d2a9
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.0-py3-none-any.whl

Download URL lazy_static-1.1.0-py3-none-any.whl
Size 3.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e019e4bbe37ae2118fb3693b78716eccf94ec3693b0e3c2408aba69edb0a2ee2
BLAKE2b-256 checksum
How to use checksums
a154e31dfba5f20da5fe5383f4207e4e937ee709d044fdda3f1606d2131e076b
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.0

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.

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