Lazy strings¶
Based on speaklater: https://github.com/mitsuhiko/speaklater.
A lazy string is an object that behaves almost exactly like a string
but where the value is not computed until needed. To define a lazy
string you specify a function that produces a string together with the
appropriate arguments for that function. Sage uses lazy strings in
sage.misc.misc
so that the filenames for SAGE_TMP (which
depends on the pid of the process running Sage) are not computed when
importing the Sage library. This means that when the doctesting code
imports the Sage library and then forks, the variable SAGE_TMP depends
on the new pid rather than the old one.
EXAMPLES:
sage: from sage.misc.lazy_string import lazy_string
sage: L = []
sage: s = lazy_string(lambda x: str(len(x)), L)
sage: L.append(5)
sage: s
l'1'
Note that the function is recomputed each time:
sage: L.append(6)
sage: s
l'2'
-
sage.misc.lazy_string.
is_lazy_string
(obj)¶ Checks if the given object is a lazy string.
EXAMPLES:
sage: from sage.misc.lazy_string import lazy_string, is_lazy_string sage: f = lambda: "laziness" sage: s = lazy_string(f) sage: is_lazy_string(s) True
-
sage.misc.lazy_string.
lazy_string
(func, *args, **kwargs)¶ Creates a lazy string by invoking func with args.
EXAMPLES:
sage: from sage.misc.lazy_string import lazy_string sage: f = lambda: "laziness" sage: s = lazy_string(f); s l'laziness'