Inline Conditionals?
Robert Brewer
fumanchu at amor.org
Thu Aug 26 16:42:44 EDT 2004
Alex Martelli wrote:
> Antoon Pardon <apardon at forel.vub.ac.be> 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
fumanchu at amor.org
More information about the Python-list
mailing list