> > Why are we trying to write one-liners in the first place? Conditional > expressions are a rare spice that should be used sparingly. No matter the > syntax, three of them nested together isn't readable code, it's a puzzle. > Why are they a rare spice that should be used sparingly? Is it in line with python best practices? Anything to support this?
I think python list comprehensions is a direct antithesis to what you are saying. I thought they are considered to be a good and preferable practice, even nested cases. We even got a new operator “:=“ to help us with those beautiful one-liners (or not) to move towards aesthetics and brevity. Also, I disagree that the referred code isn’t readable. My point is exactly that such code is not readable and is a puzzle if current conditional expression is used. While having a more elegant if-else expression, this would not be a case. Combination of list comprehensions, “:=“ operator and good conditional 1-liners could be an excellent toolkit to do complex things in simple, readable and brief manner. Now I think all is there, except if-else expression does not satisfy in either of those dimensions, namely brevity & readability (maybe it is better readable than C's for someone with major in languages…?). So I am just trying to gauge if anyone else feels this way. As I already said, my code would look fairly differently if anything similar to what I am proposing existed. And I do not have a bias towards C or C++, 99% of what I do is python so if anything I am biased towards how python does things, not the other way round. Is there any place > On 17 Jul 2023, at 23:09, Ned Batchelder <[email protected]> wrote: > > On 7/17/23 1:10 PM, Dom Grigonis wrote: >> This whole thing started from dict’s `get` extension: >> def get_substitute(self, key, default=None, subs=()): >> return key in self ? (self[key] := val in subs ? subs[val] : val) : >> default >> I dare you to do a 1-liner with current if-else. >> > Why are we trying to write one-liners in the first place? Conditional > expressions are a rare spice that should be used sparingly. No matter the > syntax, three of them nested together isn't readable code, it's a puzzle. > > --Ned. > > > >> >> >> >> >>> On 17 Jul 2023, at 18:09, Rob Cliffe <[email protected] >>> <mailto:[email protected]>> wrote: >>> >>> >>> >>> 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? >> >> >>> 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/Q4HZ5ME6V473L25AV33BA6C7JMXTI2PJ/ >> >> <https://mail.python.org/archives/list/[email protected]/message/Q4HZ5ME6V473L25AV33BA6C7JMXTI2PJ/> >> Code of Conduct: http://python.org/psf/codeofconduct/ >> <http://python.org/psf/codeofconduct/> > _______________________________________________ > 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/IBDNP5LCVXRO5FXMCGD75J5OVO7BCGQW/ > Code of Conduct: http://python.org/psf/codeofconduct/
_______________________________________________ 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/DD3RDWQVU7C6RQLKWJ6GNLHBYTFMYKJR/ Code of Conduct: http://python.org/psf/codeofconduct/
