[Python-Dev] Adding a conditional expression in Py3.0 (original) (raw)
Gareth McCaughan gmccaughan at synaptics-uk.com
Fri Sep 23 14:40:54 CEST 2005
- Previous message: [Python-Dev] Adding a conditional expression in Py3.0
- Next message: [Python-Dev] Adding a conditional expression in Py3.0
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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.
Neither sounds very natural to me; conditional expressions don't occur much in English. It's much more common to hear something like
My dog is happy if he has a bone; otherwise he's sad.
And that would correspond to a postfix conditional operator on statements, not expressions; Perl and Ruby have this, but it doesn't seem very Pythonic.
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".
Which is one reason why I don't like the proposed "consequent if antecedent else alternative" syntax; it looks like a statement modifier when it isn't really one. When you're forced (by the parens) to read it correctly, you stop liking it! :-)
*
Conditional expressions do occur in English, though, and they do generally have the consequent at the front. But ... they also generally have the "else-marker" right at the end. "Buy some pork if it's going cheap, and some beef if not." "You can divide an angle into N equal parts with ruler and compasses if N is a power of 2 times a product of distinct Fermat primes, but not if N has any other form." "I sleep all night and work all day."
I don't think a syntax that authentically mirrors the structure of conditional expressions in English is likely to be very good.
x = (123 if p==q; 321 otherwise)
x = (123 if p==q, else 321 if not)
x = (123 if p==q; else 321 if r==s; else 999 if not)
:-)
-- g
- Previous message: [Python-Dev] Adding a conditional expression in Py3.0
- Next message: [Python-Dev] Adding a conditional expression in Py3.0
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]