Thanks, thats truly useful to keep in mind.
> On 20 Jul 2023, at 09:48, anthony.flury <[email protected]> wrote: > > > The challenge with anything like > > result = default if bar is None else bar > > or > result = bar if bar else default > > or even > > result = bar is None ? default : bar > > > is that sure default is only evaluated once - but in all cases 'bar' is > evaluated twice, and if bar is a function call then no parser is clever > enough to diagnose all possible side effects of calling bar() twice, or even > detect that bar can be cached, so it will always be called twice. > > 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. > > > > ------ Original Message ------ > From: "Rob Cliffe via Python-ideas" <[email protected] > <mailto:[email protected]>> > To: "Dom Grigonis" <[email protected] <mailto:[email protected]>>; > "Jothir Adithyan" <[email protected] <mailto:[email protected]>> > Cc: [email protected] <mailto:[email protected]> > Sent: Monday, 17 Jul, 23 At 16:09 > Subject: [Python-ideas] Re: Proposal for get_or function in Python > dictionaries > > > > > On 15/07/2023 21:08, Dom Grigonis wrote: > > > Just to add. I haven’t thought about evaluation. Thus, to > prevent evaluation of unnecessary code, introduction of C-style > expression is needed anyways: > > > > > 1. result = bar is None ? default : bar > > > > > > > > The below would have to evaluate all arguments, thus not achieving > benefits of PEP505. > > > > > 2. result = ifelse(bar is None, default, bar) > > > > > > > > > > > So I would vote for something similar to: > > > > > > result = bar is None ? default : bar > > > > > > > > > Where default and bar is only evaluated if needed. Although not to > the extent as initially intended, it would offer satisfiable > solutions to several proposals. > > > > Well, default is only evaluated if needed; bar is always evaluated. > What is wrong with the Python equivalent > > result = default if bar is None else bar > or if you prefer > result = bar if bar is not None else default > > Perhaps you didn't know about this construction? > It does exactly the same as the C version and is more readable. Or > am I missing something? > Best wishes > Rob Cliffe > > _______________________________________________ > > Python-ideas mailing list -- [email protected] > <mailto:[email protected]> > > To unsubscribe send an email to [email protected] > <mailto:[email protected]> > > https://mail.python.org/mailman3/lists/python-ideas.python.org/ > <https://mail.python.org/mailman3/lists/python-ideas.python.org/> > > Message archived at > https://mail.python.org/archives/list/[email protected]/message/4QBAYBAT6EZFILNS2MCK3D6XW4LNRDZ5/ > > <https://mail.python.org/archives/list/[email protected]/message/4QBAYBAT6EZFILNS2MCK3D6XW4LNRDZ5/> > > Code of Conduct: http://python.org/psf/codeofconduct/ > <http://python.org/psf/codeofconduct/> > > > > -- <br>Anthony Flury<br>[email protected] > <mailto:[email protected]>
_______________________________________________ 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/PUKJNAGRRUGW3V4DUWNSQ3BZRLUJH5YT/ Code of Conduct: http://python.org/psf/codeofconduct/
