python syntax for conditional is unfortunate

Cameron Simpson cs at zip.com.au
Tue Sep 23 20:24:54 EDT 2008


On 23Sep2008 19:52, Neal Becker <ndbecker2 at gmail.com> wrote:
| In hindsight, I am disappointed with the choice of conditional syntax.
| I know it's too late to change.  The problem is
| 
| y = some thing or other if x else something_else
| 
| When scanning this my eye tends to see the first phrase and only
| later notice that it's conditioned on x (or maybe not notice at all!).
| Particularly if 'some thing or other' is long or complicated.

Personally, I think this is deliberate (the wordiness and ordering, not the
reading difficulty). Plenty of people dislike C's ternary b?x:y operator,
presumably for the same reasons.

A good coder will present things clearly. For trivial stuff the one line
form may be fine, and for longer stuff then this:

  y = some thing or other \
      if x \
      else something_else

or:

  if x:
      y = something or other
  else:
      y = something_else

should appear. If it's your code, this is up to you. If it's another's,
well anyone can write unreadable code...

Cheers,
-- 
Cameron Simpson <cs at zip.com.au> DoD#743
http://www.cskk.ezoshosting.com/cs/

Principles have no real force except when one is well fed.      - Mark Twain



More information about the Python-list mailing list