Python Glossary Русский

Python 3.5+linecachefunction

linecache.lazycache()

reading lines

Registers a way to fetch the source of a module that is not on disk (zip, frozen) without reading it right away.

Syntax

linecache.lazycache(filename, module_globals)

A plain getline looks for the file on disk and returns '' for a module inside a zip archive unless module_globals is passed. lazycache remembers the loader from the module's globals, and later getline calls for that name work without module_globals. It returns False for names like '<stdin>' and when the module has no loader with get_source.

Examples

import linecache, sys, zipfile
with zipfile.ZipFile('lib.zip', 'w') as z:
    z.writestr('zmod.py', 'def f():\n    return 42\n')
sys.path.insert(0, 'lib.zip')
import zmod
print(repr(linecache.getline(zmod.__file__, 1)))  # → ''
print(linecache.lazycache(zmod.__file__, vars(zmod)))  # → True
print(repr(linecache.getline(zmod.__file__, 1)))  # → 'def f():\n'
print(linecache.lazycache('<stdin>', {}))  # → False

See also

Open in the full glossarydocs.python.orgReport an inaccuracy