For review: PEP 308 - If-then-else expression

Carlos Ribeiro cribeiro at mail.inet.com.br
Sat Feb 8 16:24:59 EST 2003


On Saturday 08 February 2003 02:11 pm, Michele Simionato wrote:
> [snip]
> ...
> or the more pythonic "when"
>
>  if (when callable(obj) obj() else default):
>
> would be less confusing. I think introducing a ternary operator is
> worthwhile but one needs a new keyword to avoid confusion
> with the "if" statement.

Lets us two ways to write conditional expressions using 'when':

1) if (when callable(obj): obj() else default):

-> In this case, I think that the colon is needed after the condition; it does 
not seem right to have the condition and the true branch right next to each 
other without some explicit delimiter. The comma would work fine here.

-> As far as I understand it, this proposal requires 'when' to become a 
reserved identifier; this makes it less likely to be accepted. On the other 
hand, I think that few programs would ever use a variable named 'when', so 
this problem may be irrelevant in the end.


2) if (obj() when callable(obj) else default):

-> This one follows the steps of Guido's proposal, but it looks a little bit 
better than the original proposal, as it does not reuse 'if'. In this case, 
the colon seem to be out of place, and I think it's better without it.

-> In this case I believe that 'when' does not need to be a reserved 
identifier. But I'm not aware of all the details involved, I'm just working 
with analogies to other constructs.


In both cases, I think that the parenthesis makes the expression clearer. In 
my opinion, it should be required.

And last... the else clause has to be required. I don't see any sense in 
having an implicit default value for it - it breaks the spirit of the 
conditional operator (btw, it's not called 'ternary operator' by some people 
without any reason, isn't it?)


Carlos Ribeiro
cribeiro at mail.inet.com.br






More information about the Python-list mailing list