[Python-Dev] vox populii illiterati

Raymond Hettinger python@rcn.com
Mon, 10 Feb 2003 12:36:42 -0500


From: "holger krekel" <pyth@devel.trillke.net>
> IIRC nobody on c.l.py has come up with a real need
> for short-circuiting with the ternary op, anyway. 

Here's a copy of my reply to Aahz and c.l.py:

It is necessary when:
1. One of the alternatives has a side-effect; or
2. one or both alternatives are time consuming; or
3. the validity of the alternatives depends on the outcome
    of the test.

#  Example where all three reasons apply
data = isinstance(source, str)  ??   source.readlines()  ||  source.split()

1. readlines() moves the file pointer
2. for long sources, both alternatives take time
3. split() is only valid for strings and readlines() is only
    valid for file objects.


Raymond Hettinger