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"
29.11.17 08:08, Steven D'Aprano пише:
Perl is hardly the only language with null-coalescing operators -- we might better describe ?? as being familiar to C#, PHP, Swift and Dart. That's two mature, well-known languages and two up-and-coming languages.
What is the syntax of the ternary operator in these languages?
All four use:
condition ? first : second
for the ternary if operator.
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
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
On 29 November 2017 at 22:38, Stephan Houben
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
29.11.17 15:01, Stephan Houben пише:
What about a more general:
A if
B else C which would allow
A if is not None else C
but also e.g.
A if >= 1 else 0
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
On Wed, Nov 29, 2017 at 3:12 PM Serhiy Storchaka
29.11.17 15:01, Stephan Houben пише:
What about a more general:
A if
B else C which would allow
A if is not None else C
but also e.g.
A if >= 1 else 0
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
== B else C.
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"
On Thu, Nov 30, 2017 at 8:56 AM, Stephan Houben
Op 29 nov. 2017 22:35 schreef "Greg Ewing"
: 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
B else C proposal the evaluation of the implicit 'it' is never conditional in that way.
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
On 29 November 2017 at 22:38, Stephan Houben
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".
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:
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".
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
On 2017-11-29 21:27, Greg Ewing wrote:
Nick Coghlan 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".
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.
It has the disadvantage that it's quite long. How about "alt" (abbreviation for "alternatively"): x alt y
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
On 2017-11-29 21:27, Greg Ewing wrote:
Nick Coghlan 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".
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.
It has the disadvantage that it's quite long. How about "alt" (abbreviation for "alternatively"):
x alt y
_______________________________________________ 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 Wed, 29 Nov 2017 at 13:28 Greg Ewing
Nick Coghlan 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".
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.
In terms of syntax, this is my favourite one so far.
On Thu, Nov 30, 2017 at 12:24 PM, Brett Cannon
On Wed, 29 Nov 2017 at 13:28 Greg Ewing
wrote: Nick Coghlan 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".
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.
In terms of syntax, this is my favourite one so far.
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