internal/tools/: lazy-1.6 metadata and description

Homepage Simple index

Lazy attributes for Python objects

author Stefan H. Holek
  • Development Status :: 5 - Production/Stable
  • Intended Audience :: Developers
  • License :: OSI Approved :: BSD License
  • Operating System :: OS Independent
  • Programming Language :: Python
  • Programming Language :: Python :: 2
  • Programming Language :: Python :: 3
description_content_type text/x-rst
keywords decorator,lazy,lazy attribute,descriptor,property
license BSD-2-Clause
  • Documentation,
provides_extras docs
  • sphinx ==5.3.0 ; extra == 'docs'
  • sphinx-rtd-theme ==1.0.0 ; extra == 'docs'
  • mypy ; extra == 'mypy'
requires_python !=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,>=2.7
File Tox results History
5 KB
Python Wheel
10 KB

Package Contents


A decorator to create lazy attributes.


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.


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


The class below creates its store resource lazily:

from lazy import lazy

class FileUploadTmpStore(object):

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

    def put(self, uid, fp):, fp)

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

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

Another application area is caching:

class PersonView(View):

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

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


For further details please refer to the API Documentation.


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 test in tox.ini. [stefan]

  • Remove deprecated test_suite from [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 [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.