Re: Reinventing a more limited wheel
I'm curious about the example thames posted. Is
results = [(x, f(x), x/f(x)) for x in input_data if f(x) > 0]
really any slower than
results = [(x, y, x/y) for x in input_data if (y := f(x)) > 0] ?
In the first expression, I can see that f(x) could potentially be evaluated three times, but would that actually happen? The example implicitly assumes that f(x) returns the same value each time it's evaluated. If that's the case, is the interpreter not smart enough to cache the result of f(x) for later use? (And if that's not the case - for example if f(x) returned x+time.time() - then the two expressions above aren't actually equivalent.)