Re: [Python-ideas] [Python-Dev] What's the status of PEP 505: None-aware operators?

What about more English-like syntax: X or else Y E.g. cache.get(foo) or else expensive_call(foo) Stephan Op 29 nov. 2017 12:54 schreef "Serhiy Storchaka" <storchaka@gmail.com>: 29.11.17 11:45, Steven D'Aprano пише: On Wed, Nov 29, 2017 at 09:14:12AM +0200, Serhiy Storchaka wrote:
If all four use ?, it is natural that in operators which are shortcuts of the ternary operator they use ?. But in Python the bar of introducing ? is higher. _______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/

On 29 November 2017 at 22:38, Stephan Houben <stephanh42@gmail.com> wrote:
What about more English-like syntax:
X or else Y
The problem with constructs like this is that they look like they should mean the same thing as "X or Y". Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia

What about a more general: A if <binary_relation> B else C which would allow A if is not None else C but also e.g. A if >= 1 else 0 Stephan Op 29 nov. 2017 13:41 schreef "Nick Coghlan" <ncoghlan@gmail.com>:

29.11.17 15:01, Stephan Houben пише:
This look the most "natural" to me. I.e. the least "unnatural". If we even will introduce a new special syntax I will prefer this syntax. The only disadvantage is that this will prevent introducing "angular parenthesis" in future: A if <X, Y, Z> == B else C.

On Wed, Nov 29, 2017 at 3:12 PM Serhiy Storchaka <storchaka@gmail.com> wrote:
Also, how do you treat more complex conditions? do you prohibit them? A if >= 0 and < 2 else 0 B if >= 0 or < -1 else -1 C if and B else A # i.e. C if C and B else E D if -y else E # is this an unary minus or a binary operation? If any of these is allowed, it might confuse people. It also break the ability to extract subexpression to variables. (I kinda like this idea, but it doesn't look like a small, simple change to the language, whereas ?? does). Elazar

Stephan Houben wrote:
A if is not None else C
Reading that gives me the feeling that something in my brain has slipped a tooth. It would read better with some kind of pronoun in there: A if it is not None else C Hypercard's Hypertalk had a special variable "it" that worked sort of like that. -- Greg

Op 29 nov. 2017 22:35 schreef "Greg Ewing" <greg.ewing@canterbury.ac.nz>: It would read better with some kind of pronoun in there: A if it is not None else C Hypercard's Hypertalk had a special variable "it" that worked sort of like that. I considered that, but there are two issues. 1. Backward-incompatible change 2. The semantics of A if B else C now depends on if B contains 'it', in which case A gets evaluated unconditionally and prior to B. What if evaluation of 'it' is itself conditional, e.g. print("hello") if a or it else print("goodbye") Note that in the A if <binary_relation> B else C proposal the evaluation of the implicit 'it' is never conditional in that way. Stephan -- Greg _______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/

On Thu, Nov 30, 2017 at 8:56 AM, Stephan Houben <stephanh42@gmail.com> wrote:
The only way would be for "it" to be a keyword. You can't misinterpret "A if pass is not None else C", or "A if def is not None else C", as they're currently syntax errors. Obviously we don't want the word "it" to become a keyword (massive breakage right there), and I'm pretty sure there's no existing keyword that actually makes sense here, so this proposal would depend on coming up with a useful word that wouldn't collide too much. I'm -0.9 unless someone can find the perfect word. ChrisA

On 29 November 2017 at 12:41, Nick Coghlan <ncoghlan@gmail.com> wrote:
Keyword based and multi-word approaches also break down much faster when you get more terms. X or else Y looks OK (ignoring Nick's comment - I could pick another keyword-based proposal, but I'm too lazy to look for one I like), but when you have 4 options, X or else Y or else Z or else W the benefit isn't as obvious. Use lower-case and longer names item_one or else item_two or else list_one[the_index] or dict_one['key_one'] and it becomes just a muddle of words. Conversely, punctuation-based examples look worse with shorter variables and with expressions rather than identifiers: item_one ?? item_two ?? another_item ?? one_more_possibility vs x ?? y[2] ?? kw['id'] ?? 3 + 7 IMO, this is a case where artificial examples are unusually bad at conveying the actual feel of a proposal. It's pretty easy to turn someone's acceptable-looking example into an incomprehensible mess, just by changing variable names and example terms. So I think it's critically important for any proposal along these lines (even just posts to the mailing list, and definitely for a PEP), that it's argued in terms of actual code examples in current projects that would reasonably be modified to use the proposed syntax. And people wanting to be particularly honest in their proposals should probably include both best-case and worst-case examples of readability. Paul PS Also, I want a pony. (I really do understand that the above is not realistic, but maybe I can hope that at least anyone writing a PEP take it into consideration :-))

Nick Coghlan wrote:
How about: x otherwise y It looks different enough from "or" that you're not going to accidentally read it that way when skimming. The meaning is something you'll have to learn if you don't know, but at least it's a word that can be googled for. -- Greg

I, too, think that this is too special a case for a dedicated syntax but so far I prefer: x None-or y The meaning is more clear to me. None-or is like regular or except that y is evaluated only if x is None. On Thu, Nov 30, 2017 at 2:35 AM, MRAB <python@mrabarnett.plus.com> wrote:

On Thu, Nov 30, 2017 at 12:24 PM, Brett Cannon <brett@python.org> wrote:
Was the issue that "or else" looks too similar or looks too similar but acts very different? I'm interpreting the behavior like a(n ab)use of "or", just where it's looking for None. Am I overlooking some conditions? x = False y = 'something' x or y # 'something' x or else y # False x = None x or else y # 'something' Overlooking 'else' when reading would be surprising, though might be a source of confusion for newcomers when "x or else y" and "x or y" usually act identically, kinda like "x == y" and "x is y" (for small integers). Nick

On 29 November 2017 at 22:38, Stephan Houben <stephanh42@gmail.com> wrote:
What about more English-like syntax:
X or else Y
The problem with constructs like this is that they look like they should mean the same thing as "X or Y". Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia

What about a more general: A if <binary_relation> B else C which would allow A if is not None else C but also e.g. A if >= 1 else 0 Stephan Op 29 nov. 2017 13:41 schreef "Nick Coghlan" <ncoghlan@gmail.com>:

29.11.17 15:01, Stephan Houben пише:
This look the most "natural" to me. I.e. the least "unnatural". If we even will introduce a new special syntax I will prefer this syntax. The only disadvantage is that this will prevent introducing "angular parenthesis" in future: A if <X, Y, Z> == B else C.

On Wed, Nov 29, 2017 at 3:12 PM Serhiy Storchaka <storchaka@gmail.com> wrote:
Also, how do you treat more complex conditions? do you prohibit them? A if >= 0 and < 2 else 0 B if >= 0 or < -1 else -1 C if and B else A # i.e. C if C and B else E D if -y else E # is this an unary minus or a binary operation? If any of these is allowed, it might confuse people. It also break the ability to extract subexpression to variables. (I kinda like this idea, but it doesn't look like a small, simple change to the language, whereas ?? does). Elazar

Stephan Houben wrote:
A if is not None else C
Reading that gives me the feeling that something in my brain has slipped a tooth. It would read better with some kind of pronoun in there: A if it is not None else C Hypercard's Hypertalk had a special variable "it" that worked sort of like that. -- Greg

Op 29 nov. 2017 22:35 schreef "Greg Ewing" <greg.ewing@canterbury.ac.nz>: It would read better with some kind of pronoun in there: A if it is not None else C Hypercard's Hypertalk had a special variable "it" that worked sort of like that. I considered that, but there are two issues. 1. Backward-incompatible change 2. The semantics of A if B else C now depends on if B contains 'it', in which case A gets evaluated unconditionally and prior to B. What if evaluation of 'it' is itself conditional, e.g. print("hello") if a or it else print("goodbye") Note that in the A if <binary_relation> B else C proposal the evaluation of the implicit 'it' is never conditional in that way. Stephan -- Greg _______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/

On Thu, Nov 30, 2017 at 8:56 AM, Stephan Houben <stephanh42@gmail.com> wrote:
The only way would be for "it" to be a keyword. You can't misinterpret "A if pass is not None else C", or "A if def is not None else C", as they're currently syntax errors. Obviously we don't want the word "it" to become a keyword (massive breakage right there), and I'm pretty sure there's no existing keyword that actually makes sense here, so this proposal would depend on coming up with a useful word that wouldn't collide too much. I'm -0.9 unless someone can find the perfect word. ChrisA

On 29 November 2017 at 12:41, Nick Coghlan <ncoghlan@gmail.com> wrote:
Keyword based and multi-word approaches also break down much faster when you get more terms. X or else Y looks OK (ignoring Nick's comment - I could pick another keyword-based proposal, but I'm too lazy to look for one I like), but when you have 4 options, X or else Y or else Z or else W the benefit isn't as obvious. Use lower-case and longer names item_one or else item_two or else list_one[the_index] or dict_one['key_one'] and it becomes just a muddle of words. Conversely, punctuation-based examples look worse with shorter variables and with expressions rather than identifiers: item_one ?? item_two ?? another_item ?? one_more_possibility vs x ?? y[2] ?? kw['id'] ?? 3 + 7 IMO, this is a case where artificial examples are unusually bad at conveying the actual feel of a proposal. It's pretty easy to turn someone's acceptable-looking example into an incomprehensible mess, just by changing variable names and example terms. So I think it's critically important for any proposal along these lines (even just posts to the mailing list, and definitely for a PEP), that it's argued in terms of actual code examples in current projects that would reasonably be modified to use the proposed syntax. And people wanting to be particularly honest in their proposals should probably include both best-case and worst-case examples of readability. Paul PS Also, I want a pony. (I really do understand that the above is not realistic, but maybe I can hope that at least anyone writing a PEP take it into consideration :-))

Nick Coghlan wrote:
How about: x otherwise y It looks different enough from "or" that you're not going to accidentally read it that way when skimming. The meaning is something you'll have to learn if you don't know, but at least it's a word that can be googled for. -- Greg

I, too, think that this is too special a case for a dedicated syntax but so far I prefer: x None-or y The meaning is more clear to me. None-or is like regular or except that y is evaluated only if x is None. On Thu, Nov 30, 2017 at 2:35 AM, MRAB <python@mrabarnett.plus.com> wrote:

On Thu, Nov 30, 2017 at 12:24 PM, Brett Cannon <brett@python.org> wrote:
Was the issue that "or else" looks too similar or looks too similar but acts very different? I'm interpreting the behavior like a(n ab)use of "or", just where it's looking for None. Am I overlooking some conditions? x = False y = 'something' x or y # 'something' x or else y # False x = None x or else y # 'something' Overlooking 'else' when reading would be surprising, though might be a source of confusion for newcomers when "x or else y" and "x or y" usually act identically, kinda like "x == y" and "x is y" (for small integers). Nick
participants (11)
-
Brett Cannon
-
Chris Angelico
-
Elazar
-
electronnn@gmail.com
-
Greg Ewing
-
MRAB
-
Nick Coghlan
-
Nick Timkovich
-
Paul Moore
-
Serhiy Storchaka
-
Stephan Houben