Leave off "else" in ternary expression
The null-coalescing discussion made me think about the current ternary "x = a if b else c" expression. In normal "if / else" clauses, the "else" is optional. I propose doing the same thing with ternary expressions (although I don't know what the result would be called, a "binary expression"?) The idea would be to allow this syntax: x = a if b Which would be equivalent to: x = a if b else x I think this would be useful syntax. In particular, I see it being useful for default value checking, but can also be used to override the result of particular corner cases from functions or methods..
On 10/28/2016 01:28 PM, Todd wrote:
The idea would be to allow this syntax:
x = a if b
Which would be equivalent to:
x = a if b else x
What if x has not been defined yet? -- Bernardo Sulzbach http://www.mafagafogigante.org/ mafagafogigante@gmail.com
On Fri, Oct 28, 2016 at 11:36 AM, Bernardo Sulzbach < mafagafogigante@gmail.com> wrote:
On 10/28/2016 01:28 PM, Todd wrote:
The idea would be to allow this syntax:
x = a if b
Which would be equivalent to:
x = a if b else x
What if x has not been defined yet?
Same as "x = a if b else x", it would raise a NameError.
This seems pretty nonsensical to me. Ternaries are not only used in simple assignments. E.g. 'myfunc(a, b if pred else c, d)' is common and obvious. 'myfunc(a, b if pred, d)' is strange with no obvious semantics. On Oct 28, 2016 11:29 AM, "Todd" <toddrjen@gmail.com> wrote:
The null-coalescing discussion made me think about the current ternary "x = a if b else c" expression. In normal "if / else" clauses, the "else" is optional. I propose doing the same thing with ternary expressions (although I don't know what the result would be called, a "binary expression"?)
The idea would be to allow this syntax:
x = a if b
Which would be equivalent to:
x = a if b else x
I think this would be useful syntax. In particular, I see it being useful for default value checking, but can also be used to override the result of particular corner cases from functions or methods..
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
That is a good point. Nevermind then. On Fri, Oct 28, 2016 at 12:20 PM, David Mertz <mertz@gnosis.cx> wrote:
This seems pretty nonsensical to me. Ternaries are not only used in simple assignments.
E.g. 'myfunc(a, b if pred else c, d)' is common and obvious.
'myfunc(a, b if pred, d)' is strange with no obvious semantics.
On Oct 28, 2016 11:29 AM, "Todd" <toddrjen@gmail.com> wrote:
The null-coalescing discussion made me think about the current ternary "x = a if b else c" expression. In normal "if / else" clauses, the "else" is optional. I propose doing the same thing with ternary expressions (although I don't know what the result would be called, a "binary expression"?)
The idea would be to allow this syntax:
x = a if b
Which would be equivalent to:
x = a if b else x
I think this would be useful syntax. In particular, I see it being useful for default value checking, but can also be used to override the result of particular corner cases from functions or methods..
_______________________________________________ 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 Fri, Oct 28, 2016 at 11:28:16AM -0400, Todd wrote:
The null-coalescing discussion made me think about the current ternary "x = a if b else c" expression. In normal "if / else" clauses, the "else" is optional. I propose doing the same thing with ternary expressions (although I don't know what the result would be called, a "binary expression"?)
The idea would be to allow this syntax:
x = a if b
Which would be equivalent to:
x = a if b else x
I think this would be useful syntax. In particular, I see it being useful for default value checking, but can also be used to override the result of particular corner cases from functions or methods..
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
To me, it is completely un-intuitive that it would work like that. It seems to parse as (x = a) if b instead as x = (a if b) That would make an assignment part of an expression, which seems very un-Pythonic. We also do not have if (x = a): pass When I first read your proposal, I assumed it would mean "use `None` as default `else` expression". Upon reading it, I am quite certain that the semantics you propose are not going to make it into Python. (But then again, I'm not the BDFL).
participants (4)
-
Bernardo Sulzbach
-
David Mertz
-
Sjoerd Job Postmus
-
Todd