Apologies for the inactive post. > Why the double condition? The existing definition of "condition1 and > not condition2" already guarantees the short-circuiting, and what > you're effectively creating is two pieces of a single condition. Just for clarity sir. I wanted to make clear the desired behavior. 😀
> Okay, now this starts to look somewhat interesting. But what you'd be > creating here would be a feature of specific language constructs, and > probably wouldn't work everywhere. Yes. I think it would be too hasty to add EMPTY to other language constructs right now. > How close is this to what you were thinking of? The conditional omission part is exactly what I'm thinking of. I made a mistake in my original proposal. To keep all the information organized, I'm going to keep a canonical version of the proposal on the link below. https://hackmd.io/CDqvdxF_QY-STrz0wQy83g Please give it a new read. I tried to make it as clear as possible in this new version. Here are the revisions I've made compared to the original thanks to your discussions. That's without the two-part condition. You can always put "expr unless x and not y" if you need both parts. > I don't even know what you mean by that even after reading your > examples, sorry. Chris A. gave an excellent interpretation of what I mean: "Inside a list/dict/set/tuple display, or inside an argument list, elements can be conditionally omitted by providing a predicate." > On Mon, Jun 3, 2019 at 9:25 PM Chris Angelico <[email protected]> wrote: > On Tue, Jun 4, 2019 at 10:58 AM James Lu <[email protected]> wrote: > > > > `if-unless` expressions in Python > > > > if condition1 expr unless condition2 > > > > is an expression that roughly reduces to > > > > expr if condition1 and not condition2 else EMPTY > > > > This definition means that expr is only evaluated if `condition1 and not > > condition2` evaluates to true. It also means `not condition2` is only > > evaluated if `condition1` is true. > > Why the double condition? The existing definition of "condition1 and > not condition2" already guarantees the short-circuiting, and what > you're effectively creating is two pieces of a single condition. > > > # EMPTY > > > > EMPTY is not actually a real Python value-- it's a value that collapses > > into nothing when used inside a statement expression: > > > > print([ > > if False never_called() unless False, > > if False never_called() unless False, > > ]) # => [] > > > > print([ > > 3, > > if False never_called() unless False, > > if False never_called() unless False, > > 2, > > if True 5 unless False, > > 4 > > ]) # => [3, 2, 5, 4] > > > > > > EMPTY is neither a constant exposed to the Python runtime nor a symbol. > > It's a compiler-internal value. > > Okay, now this starts to look somewhat interesting. But what you'd be > creating here would be a feature of specific language constructs, and > probably wouldn't work everywhere. > > > # Use cases > > > > Assertions. > > > > assert if condition1 predicate(object) unless condition2 > > > > (This would be more readable with assert expressions.) > > Honestly not sure what this does that's better than ordinary > expressions. Can you give some concrete examples of how you would use > this, and what the equivalent code for current Pythons would look > like? > > > Macros. > > > > # Equivalent syntax in existing Python > > > > As a statement: > > > > if condition1 and not condition2: predicate(object) > > > > predicate(object) if condition1 and not condition2 > > Adding "else None" would make this legal Python syntax right now, but > for very good reasons, is not how most people write code. > > So, looking just at the "EMPTY" part. Allow me to reword your proposal > into what I think you're saying; is this what you're proposing? > > ## New "unless" construct for list displays and argument lists ## > > Inside a list/dict/set/tuple display, or inside an argument list, > elements can be conditionally omitted by providing a predicate. > > lst = [f1(), f3() unless f2(), f4()] > > The functions will be called in the order indicated. If f2() returns a > falsey value, f3() will not be evaluated, and the list will have only > two elements in it. > > ##### > > That's without the two-part condition. You can always put "expr unless > x and not y" if you need both parts. > > How close is this to what you were thinking of? > > ChrisA > _______________________________________________ > Python-ideas mailing list > [email protected] > https://mail.python.org/mailman/listinfo/python-ideas > 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/XPUSUGTUI2ORUXCDNJEWU5AEORJBW45S/ Code of Conduct: http://python.org/psf/codeofconduct/
