Builtins.lazy for lazy arguments

Good point, but lazy isn’t purposed for that. Its sole purpose is to wrap a callable and signal argument being lazy. It can be used in conjunction with other tools to achieve what you have pointed out. Something along the lines of Evaluation graphs would do. E.g.:

v = dct.get(k, default=lazy(dask.deferred(factorial, 100_000) + 1)

Or given the fact, that this case is trivial, simple lambda can do:

v = dct.get(k, default=lazy(lambda: factorial(100_000) + 1))

That is also an option.
In this specific case one could do with something like a pipe instead (see: `functools.pipe`. It didn’t go through though):

v = dct.get(k, default=lazy(pipe(factorial, partial(add, 1)), 1))

But the main point is that for complex cases there are many tools to be used in conjunction.

This is a good option, however it only works for this specific case of “default return value argument”, but lazy is more generic, e.g.:

def ifelse(cond, a, b):
    result = a if cond else b
    if isinstance(result, lazy):
        result = result()
    return result
1 Like