Re: python syntax for conditional is unfortunate

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Cameron Simpson

    Re: python syntax for conditional is unfortunate

    On 23Sep2008 19:52, Neal Becker <ndbecker2@gmai l.comwrote:
    | 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@zip.com.auD oD#743


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

    #2
    Re: python syntax for conditional is unfortunate

    Cameron Simpson <cs@zip.com.auw rites:
    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
    Parentheses are usually more robust for multi-line, where possible:

    foo = (bar
    if some_condition( )
    else baz)

    The lines can more easily be edited and rearranged without fiddling
    with backslashes at the end of every line. (For even more robustness
    at the cost of space, separate the parentheses so they're on separate
    lines from what they enclose.)

    It also addresses the original poster's complaint that there's no
    early signal of the compound nature of the expression: the opening
    parenthesis signals this.

    --
    \ “It is far better to grasp the universe as it really is than to |
    `\ persist in delusion, however satisfying and reassuring.” —Carl |
    _o__) Sagan |
    Ben Finney

    Comment

    Working...