
July 20, 2023
7:17 a.m.
On Thu, 20 Jul 2023 at 17:09, Dom Grigonis <dom.grigonis@gmail.com> wrote:
On 20 Jul 2023, at 09:48, anthony.flury <anthony.flury@btinternet.com> wrote: In Python then a better way might be
result = temp := bar() if temp else default
This way only bar() and default are evaluated and invoked once.
I presume you want a more complicated expression than just "if temp", since this is no better than "bar() or default". But if you DO want some other condition, this would be how you'd write it: result = temp if (temp := bar()) is None else default Or, better: result = bar() if result is None: result = default ChrisA