RE: Inline Conditionals?

Discussion in 'Python' started by Robert Brewer, Aug 26, 2004.

  1. Alex Martelli wrote:
    > Antoon Pardon <> wrote:
    > ...
    > > When using list comprehension not having a ternary operator can be
    > > a PITA. It is of course possible I miss something but how am I
    > > supposed to do the following:
    > >
    > > [ x.property ? foo(x) : bar(x) for x in Somelist ]

    >
    > If you HAVE to use an LC by doctor's order, the above effect might be
    > obtained by coding something like:
    >
    > [ (bar,foo)[bool(x.property)](x) for x in Somelist ]
    >
    > If your physician should relent and let you code normal
    > Python, though,
    >
    > aux = []
    > for x in Somelist:
    > if x.property:
    > aux.append(foo(x))
    > else
    > aux.append(foo(x))
    >
    > would be vastly more readable; "sparse is better than dense"
    > and any LC
    > is far too dense to be Pythonic here.


    Case in point: it makes it *vastly* easier to see the typo. What
    happened to bar()?

    But you planned that so someone else could deliver the punch line,
    didn't you? ;)


    Robert Brewer
    MIS
    Amor Ministries
     
    Robert Brewer, Aug 26, 2004
    #1
    1. Advertising

  2. Robert Brewer <> wrote:
    ...
    > > aux = []
    > > for x in Somelist:
    > > if x.property:
    > > aux.append(foo(x))
    > > else
    > > aux.append(foo(x))
    > >
    > > would be vastly more readable; "sparse is better than dense"
    > > and any LC
    > > is far too dense to be Pythonic here.

    >
    > Case in point: it makes it *vastly* easier to see the typo. What
    > happened to bar()?


    Closing hours...?


    > But you planned that so someone else could deliver the punch line,
    > didn't you? ;)


    Ahem, yes, why, sure!


    Alex
     
    Alex Martelli, Aug 26, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Abhi
    Replies:
    2
    Views:
    750
    E. Robert Tisdale
    Jul 3, 2003
  2. Alvin
    Replies:
    7
    Views:
    487
    E. Robert Tisdale
    May 6, 2005
  3. Replies:
    3
    Views:
    471
  4. Joshua Ginsberg

    Inline Conditionals?

    Joshua Ginsberg, Aug 24, 2004, in forum: Python
    Replies:
    15
    Views:
    3,854
    Martin Maney
    Sep 6, 2004
  5. jdwyer3
    Replies:
    3
    Views:
    400
    Kai-Uwe Bux
    Oct 28, 2005
Loading...

Share This Page