PEP 308: Alternative conditional operator forms

Evan Simpson evan at 4-am.com
Tue Feb 11 16:06:43 EST 2003


Michele Simionato wrote:
> As I said in some other thread, I want a clear distinction between the
> "if" statement and the conditional expression. They cannot be distinguished
> by a parenthesis only:

"Cannot" in what sense?  The grammar can handle it.

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

Properly so.  Why do you consider this to be a problem?

> And what is this with parens that has to be there? This is so 
> inconsistent with how parens are used normally in Python.

Function calls and defs require parens, tuple parens are optional, class 
bases require parens.  Parens can also group expressions.  Where is the 
consistency that is being violated, and what cost do you imagine that 
breaking it would impose?

> Moreover, I hate to call a function with
> 
> f((if C: x else: y))

Do you also hate "f((x,))" to pass a one-tuple to a function?

> and I hate the next unavoidable step 
> 
> (try: X except: Y)

"Unavoidable"?  You've lost me.

> Why to go over all this trouble when we have
> 
> C then x else y
> 
> without parenthesis, colons, if and with "then" not a keyword ?

This isn't too bad, if it can be made to work.  Having a magical 
non-keyword in the middle of an expression is hardly consistent with the 
rest of Python, though.  Between that and chaining, we can construct:

print now or then then then then then else now else "never"

In the spirit of your first objection above, we also have:

C then print x else print y

Cheers,

Evan @ 4-am







More information about the Python-list mailing list