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

Carlos Ribeiro cribeiro at mail.inet.com.br
Sun Feb 9 13:04:25 EST 2003


On Saturday 08 February 2003 21:08, Janto Dreijer wrote:
> I find it difficult to imagine a situation where the short-circuit
> behavior actually matters. Theoretically an ifelse function doesn't do
> the same thing, but practically it does: Whenever I've used ternary
> ops there weren't any side effects to evaluating both arguments.
> Neither any performance ones.
>
> Can someone give me a real world example of its usefulness?

Most potential uses are to avoid some exception being thrown: testing on an 
array index, avoiding a division by zero, negative square roots and so on.

For business applications it is also useful, because sometimes we do retrieve 
values from external sources depending upon some condition. The meta-code 
below shows the basic idea:

  zip_code = (some_zip if (some_zip != None) else find_zip(some_address))

'find_zip' may look up the zip code on some table, or even worse, using the 
Internet to do the lookup in a remote database. Short-circuiting is a big 
time saver here, as it will be whenever cache-style techniques are used.

Alas, the parenthesis above help to make the expression easier to understand, 
in my opinion, but are not actually needed.


Carlos Ribeiro
cribeiro at mail.inet.com.br








More information about the Python-list mailing list