On Thu, 20 Jul 2023 at 17:09, Dom Grigonis <[email protected]> wrote:
>
> On 20 Jul 2023, at 09:48, anthony.flury <[email protected]> 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
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/[email protected]/message/FI376QAZ7FAA7ZFJPELG73LXR5VFIHBD/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to