b.lazy_import : module documentation

Part of bzrlib

Functionality to create lazy evaluation objects.

This includes waiting to import a module until it is actually used.

Most commonly, the 'lazy_import' function is used to import other modules in an on-demand fashion. Typically use looks like:

from bzrlib.lazy_import import lazy_import
lazy_import(globals(), '''
from bzrlib import (
    errors,
    osutils,
    branch,
    )
import bzrlib.branch
''')

Then 'errors, osutils, branch' and 'bzrlib' will exist as lazy-loaded objects which will be replaced with a real object on first use.

In general, it is best to only load modules in this way. This is because it isn't safe to pass these variables to other functions before they have been replaced. This is especially true for constants, sometimes true for classes or functions (when used as a factory, or you want to inherit from them).

Class ScopeReplacer A lazy object that will replace itself in the appropriate scope.
Class ImportReplacer This is designed to replace only a portion of an import list.
Class ImportProcessor Convert text that users input into lazy import requests
Function lazy_import Create lazy imports for all of the imports in text.
def lazy_import(scope, text, lazy_import_class=None):
Create lazy imports for all of the imports in text.

This is typically used as something like:

from bzrlib.lazy_import import lazy_import
lazy_import(globals(), '''
from bzrlib import (
    foo,
    bar,
    baz,
    )
import bzrlib.branch
import bzrlib.transport
''')

Then 'foo, bar, baz' and 'bzrlib' will exist as lazy-loaded objects which will be replaced with a real object on first use.

In general, it is best to only load modules in this way. This is because other objects (functions/classes/variables) are frequently used without accessing a member, which means we cannot tell they have been used.

API Documentation for Bazaar, generated by pydoctor at 2022-06-16 00:25:16.