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.

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(f, *args, **kwargs)#

Creates a lazy string.

INPUT:

  • f, either a callable or a (format) string

  • positional arguments that are given to f, either by calling or by applying it as a format string

  • named arguments, that are forwarded to f if it is not a string

EXAMPLES:

sage: from sage.misc.lazy_string import lazy_string
sage: f = lambda x: "laziness in "+str(x)
sage: s = lazy_string(f, ZZ); s
l'laziness in Integer Ring'

Here, we demonstrate that the evaluation is postponed until the value is needed, and that the result is not cached:

sage: class C:
....:     def __repr__(self):
....:         print("determining string representation")
....:         return "a test"
sage: c = C()
sage: s = lazy_string("this is %s", c)
sage: s
determining string representation
l'this is a test'
sage: s == 'this is a test'
determining string representation
True