Skip to main content

lazy

Lazy attributes for Python objects

Package Contents

@lazy

A decorator to create lazy attributes.

Overview

Lazy attributes are computed attributes that are evaluated only once, the first time they are used. Subsequent uses return the results of the first call. They come handy when code should run

  • late, i.e. just before it is needed, and

  • once, i.e. not twice, in the lifetime of an object.

You can think of it as deferred initialization. The possibilities are endless.

Typing

The decorator is fully typed. Type checkers can infer the type of a lazy attribute from the return value of the decorated method.

Examples

The class below creates its store resource lazily:

from lazy import lazy

class FileUploadTmpStore(object):

    @lazy
    def store(self):
        location = settings.get('fs.filestore')
        return FileSystemStore(location)

    def put(self, uid, fp):
        self.store.put(uid, fp)
        fp.seek(0)

    def get(self, uid, default=None):
        return self.store.get(uid, default)

    def close(self):
        if 'store' in self.__dict__:
            self.store.close()

Another application area is caching:

class PersonView(View):

    @lazy
    def person_id(self):
        return self.request.get('person_id', -1)

    @lazy
    def person_data(self):
        return self.session.query(Person).get(self.person_id)

Documentation

For further details please refer to the API Documentation.

Changelog

2.0 - 2026-07-04

  • Remove deprecated license classifier. [stefan]

  • Upgrade GitHub workflow. [stefan]

  • Upgrade .readthedocs.yaml. [stefan]

  • Upgrade sphinx and sphinx-rtd-theme in docs extra. [stefan]

  • Require Python >= 3.5. [stefan]

1.6 - 2023-09-14

  • Implement lazy.__set_name__() which helps in cases like foo=lazy(_foo). [stefan]

  • Update tox.ini for latest tox. [stefan]

  • Add GitHub CI workflow. [stefan]

  • Add .readthedocs.yaml file. [stefan]

  • Pin sphinx and sphinx-rtd-theme versions in docs extra. [stefan]

  • Add mypy extra which installs mypy. [stefan]

  • Fix stray characters in keywords. [stefan]

1.5 - 2022-09-18

  • Allow type checkers to infer the type of a lazy attribute. Thanks to Elias Keis and Palpatineli for their contributions. [elKei24] [Palpatineli]

  • Add Python 3.8-3.11 to tox.ini. Remove old Python versions. [stefan]

  • Replace deprecated python setup.py test in tox.ini. [stefan]

  • Remove deprecated test_suite from setup.py. [stefan]

  • Move metadata to setup.cfg and add a pyproject.toml file. [stefan]

  • Include tests in sdist but not in wheel. [stefan]

1.4 - 2019-01-28

  • Add MANIFEST.in. [stefan]

  • Release as universal wheel. [stefan]

1.3 - 2017-02-05

  • Support Python 2.6-3.6 without 2to3. [stefan]

  • Add a LICENSE file. [stefan]

1.2 - 2014-04-19

  • Remove setuptools from install_requires because it isn’t. [stefan]

1.1 - 2012-10-12

  • Use functools.wraps() properly; the list of attributes changes with every version of Python 3. [stefan]

1.0 - 2011-03-24

  • Initial release.

Metadata

Release files for lazy 2.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 2.0
File Size Uploaded
lazy-2.0.tar.gz 10.6 kB Details

Built distribution (wheel)

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

Total release size: 15.9 kB

Release files / lazy-2.0.tar.gz

Download URL lazy-2.0.tar.gz
Size 10.6 kB
Tags Source
SHA-256 checksum
How to use checksums
f92ec0d32dd6bd115ddec4e3323473ebc0b682ad58c6aca78ebfd9e6d1aa5777
BLAKE2b-256 checksum
How to use checksums
c34ae1204ed0cdf2feb50768e638fda3e97363f362c9a10123c585d5056af556
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.2

Release files / lazy-2.0-py3-none-any.whl

Download URL lazy-2.0-py3-none-any.whl
Size 5.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6bf06f6a71dbe6148f3948c6388b264772493972ad22fb958d24bb52e7e7ec9b
BLAKE2b-256 checksum
How to use checksums
318211dc927238558638dcabc59b37c398ca3e1c0db935e2539d7e710e515aa0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.2

Release history Release notifications | RSS feed

This release

2.0 This release

2 release files

1.6

2 release files

1.5

2 release files

1.4

2 release files

1.3

1 release file

1.2

1 release file

1.1

1 release file

1.0

1 release file

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