PEP 308: Alternative conditional operator forms

Evan Simpson evan at 4-am.com
Wed Feb 12 18:32:05 CET 2003


Michele Simionato wrote:
> "Cannot" in the sense "such a confusion (for the reader, not for the grammar)
> cannot be allowed in Python which is intended to be a language easy to read".

Ah, you meant "ought not" or "should not".  I disagree, more of which below.

> Besides, tuple are expressions, not statements, I have nothing
> against parens in expressions.

This is a bit circular, but OK.

> If there is a precedent in the topic of converting statement to
> expressions, how to stop people asking for other conversions? 

Say "no"?  This is a slippery slope argument, and you're not alone in 
finding it persuasive, but I don't buy it.  As far as I can recall, the 
only sorts of statements that anyone has ever suggested embedding in an 
expression (on c.l.py, anyway) are assignment and conditional.  I would 
be perfectly happy to allow:

if (x = f()):
     print x

...with required parens.  I'm not about to start lobbying for it, though.

> Who is going to use "then" as a variable name ??

Few people, most likely; I meant that as a bit of fun, not a serious 
objection.

> I think the harm of having a "then" keyword is much
> *much* smaller than the harm of having statement->expression
> conversion.

The harm of a new keyword is that some code becomes 
forward-incompatible.  The "harm" of a conditional expression that looks 
like a conditional statement is ___?  If you answer, please elaborate 
beyond "it would be confusing" -- what exactly would be the consequences 
of that confusion?

>>In the spirit of your first objection above, we also have:
>>
>>C then print x else print y
> 
> Nick Vargish already answered to that.

I think you both missed my point.  I realize that this is clearly a 
SyntaxError, I just don't see this as being much different as an 
objection to "_ then _ else _" than when you raised it:

> (if C: print x
>  else: print y)
> 
> would be a syntax error!

Placing any statement in any form of conditional expression is a 
SyntaxError.  If a naive coder were tempted to try it based on the 
similarity to the if-statement syntax, they would immediately be 
informed by the compiler that they can't do that.  Checking the docs 
would (presumably) reveal the explanation to them.

Cheers,

Evan @ 4-am







More information about the Python-list mailing list