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

Carlos Ribeiro cribeiro at mail.inet.com.br
Sat Feb 8 19:32:00 EST 2003


On Saturday 08 February 2003 02:40 pm, Andrew Koenig wrote:
> James> It *might* be possible to obtain the effects of the proposed
> James> expression in a more general way by extending the semantics of
> James> the following existing construct:
>
> James>    ("html", "text")[is_html(msg)]
>
> This is the third message of this kind (by three different authors,
> I think) that has the two alternatives swapped!  This fact, by itself,
> convinces me that this particular form is a Bad Idea.  

The problem here is that 'if' constructs in any sane language are written with 
the 'true' condition first, followed by the 'false' condition. However, 
boolean values have the reverse ordering (assuming false=0, true=1). This is 
a recipe for bugs, as it relies on something IMPLICIT (ordering of boolean 
values) to obtain the correct behavior.

[btw, I thought that explicit was better than implicit, but seeing these 
suggestions leaves me wondering if this is still regarded as important. the 
same goes for the 'a and b or c' - again, abusing side effects of logic 
expressions when all you want is to state a simple choice depending upon a 
condition]

> It would be much better to write
>
>         { True: "html", False: "text" }[is_html(msg)]

I thought about something on these lines - a 'select' operator that would pick 
an element of a list or dict, using lazy evaluation rules. But if conditional 
expressions are controversial, I wonder what people will say about lazy 
evaluation...


Carlos Ribeiro
cribeiro at mail.inet.com.br





More information about the Python-list mailing list