Revision 3 as of 2005-01-20 22:58:07

Clear message

TableOfContents

The Problem

Python is sorely missing an if statement that:

In C and its derivitives,

predicate ? A : B 

This is extremely useful in programming and helps avoid copy and paste assignments. This is an example of poor code, because the assignment logic "x=" is copied and pasted, violating the software maintainbility and readability principle of OnceAndOnlyOnce:

if test:
   x=sin(cos(x))
else:
   x=cos(sin(x))

See also SwitchStatement

Current Python Idioms

There are a few ways in Python do achieve the evaluation only of the statement used when the predicate is true:

Proposed Solutions

If always returns a value

x = if test:
    A()
 else:
     B()

Discussion

Please see http://python.org/peps/pep-0308.html and google for discussions about the ternary operator (if you haven't yet).

Unable to edit the page? See the FrontPage for instructions.