
Years ago there was an interesting movement called anti-if campaign, now it's more promotional, but the concept of "anti if" may help you find ways to remove the cases where your suggest "and if" and "or if" can apply. This article is particularly well written: https://code.joejag.com/2016/anti-if-the-missing-patterns.html If you have cases where you think "and if" and "or if" can be helpful, you probably underuse oop. Le sam. 21 déc. 2019 à 20:32, Soni L. <fakedme+py@gmail.com> a écrit :
On Dec 21, 2019, at 08:41, Soni L. <fakedme+py@gmail.com> wrote:
I'd like to see the ability to do:
if x: 1 and if y: 2 or if z: 3
The truth table for these would be:
x | y | z | result 0 | _ | 0 | (none) 0 | _ | 1 | 3 1 | 0 | _ | 1,3 1 | 1 | _ | 1,2,3
and each statement is evaluated once, when encountered. (as such, y and z may not be evaluated at all, if their evaluation is not necessary to determine the outcome.)
So this is equivalent to:
if x: if y: 1, 2, 3 else: 1, 3 elif z: 3
I can see how the former saves me having to repeat the 3 three times. But the cost is being less obvious about when exactly I get a 3 so I’m forced to work it through step by step—including the confusion about the 1,0,0 case, which, as you mentioned, is only clear if you imagine putting an else at the end (although maybe you’d get used to that once you’d read
On 2019-12-21 4:15 p.m., Andrew Barnert wrote: through enough of these?). It’s even less obvious if you do throw in an elif, or just add an and if to the end (so now the condition to get there is not “x and y or z and w” but, I think, “((x and y) or z) and w”?
Does that advantage outweigh the disadvantage? Certainly not for this
example. But that’s probably because even the rewritten example is meaningless and useless. Maybe it would be different with a realistic use case, but I can’t imagine what that would be. Surely you must have some case where you really wanted this, that motivated you to propose it?
"1" and "2" and "3" are pieces of code, ofc.
it's actually more like:
if flag := x: 1 if y: 2 if flag or z: # note that "or" is short-circuiting. 3
but more efficiently implemented in bytecode without that "flag" local showing up.
it's literally meant for easing the translation of switch statements from other languages and nothing else. (well, at least the "or if" part. the "and if" part isn't very useful. maybe to reduce deep indentation I guess?) _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/NNVZQ5... Code of Conduct: http://python.org/psf/codeofconduct/