PEP308 - preference for 'x if c else y' over 'c then x else y'

Stephen Horne intentionally at blank.co.uk
Sat Feb 15 00:37:37 EST 2003


Just a minor thought to throw into the mix.

To me, the 'x if c else y' syntax extends more naturally when there
are more than two values to choose from.

Extending 'c then x else y' would require obvious nesting. It could be
nested two ways...

  if c1 then A else (if c2 then B else C)

  if c1 then (if c2 then A else B) else C

There's no pressing reason to choose one over the other, so it would
probably end with different programmers using different styles - but
(without the brackets) it would probably be annoying to encounter the
less familiar version in other peoples code - one of those 'yuck'
moments.

The '... if ... else ...' version seems to have a natural
repetition-based pattern of growth which need not rely on obvious
nesting...

  x if c1 else y if c2 else z

OK, yes, it is nesting really - but, like the C 'else if', it reads
more like a continuation of the same thing than a nesting of one thing
in another. Repetition usually seems easier to understand than
recursion, even though (or maybe because) it is really just a special
case of recursion.

-- 
steve at ninereeds dot fsnet dot co dot uk




More information about the Python-list mailing list