I would like to implement a function f
which returns two values x
and y
given an input w
. This function first computes the value of x
using w
and then y
using the computed valued of x
. However, since the computation of y
is expensive, I want the function to compute its value only when I need the result from the function.
That is, the following call to f
will not execute the part of the function which computes y
...
x, _ = f(w)
... and the following call will execute it ...
x, y = f(w)
Is it possible to define the function in this way? Of course, a straightforward alternative is to pass an extra Boolean variable into f
which determines whether y
is to be computed or not but I'm wondering if there are other ways to do this. Thanks!