Sunday, February 26, 2006

Rationale for Conditional Expression

Guido explained part of his rationale for adding a conditional expression to Python 2.5. Many users have been attracted to the subtly flawed "a and b or c" form. (It returns c if a is True and b evaluates to False.) Adding a direct form will prevent bugs in their code.

I still don't like it. The proposed form "val if test else val" is too confusing. The test does not belong in the middle. The right thing is "if test then value else val," which Guido is not doing because of the cost of introducing a "then" keyword. (It is also excruciating to get the parser to recognize the difference between a bare "if ... then ..." expression and an "if ...:" statement.)

No comments: