[Python-Dev] Adding a conditional expression in Py3.0
Greg Ewing
greg.ewing at canterbury.ac.nz
Thu Sep 22 05:09:44 CEST 2005
Gary Herron wrote:
> And in fact, one read and understands your return statement just like an
> English sentence -- word by word from beginning to end. This seems an
> argument FOR the syntax not against. Moreover, if one uses the
> proposed parenthesized syntax, even the slightly odd word order of
> "return if" is mitigated.
The reason I like "a if b else c" is because it has the
most natural word order. In English,
My dog is happy if he has a bone, else sad.
sounds much more natural than
My dog is, if he has a bone, happy, else sad.
In return statements,
return self.arg if self.arg is not None else default
looks quite all right to me. I think the fact that it does
resemble English word order so much prevents the word-soup
problem from occurring.
Interestingly, it looks *more* odd to me if parens are
included:
return (self.arg if self.arg is not None else default)
I think this is because, without the parens, I tend to read
the "if" as applying to the whole phrase "return self.arg",
not just to the "self.arg".
The English analogy is rewriting "My dog is happy if he has
a bone" as "If he has a bone, my dog is happy", which also
sounds natural, whereas "My dog is, if he has a bone, happy"
sounds unnatural.
So I still prefer "a if b else c" to any of the alternatives,
and I still think parens should not be required.
--
Greg Ewing, Computer Science Dept, +--------------------------------------+
University of Canterbury, | A citizen of NewZealandCorp, a |
Christchurch, New Zealand | wholly-owned subsidiary of USA Inc. |
greg.ewing at canterbury.ac.nz +--------------------------------------+
More information about the Python-Dev
mailing list