Inline Conditionals?

Alex Martelli aleaxit at yahoo.com
Thu Aug 26 17:09:00 EDT 2004


Paul Rubin <http://phr.cx@NOSPAM.invalid> wrote:

> aleaxit at yahoo.com (Alex Martelli) writes:
> > 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.
> 
> Hmm,
> 
>     [ x.property ? foo(x) : bar(x) for x in Somelist ]
> 
> doesn't seem too dense, unless you consider -every- LC to be too
> dense, in which case why have them?  

I don't, but I do consider almost every application of ternary to result
in too-dense code, which is why I'm ecstatic not to have them.

> more readable and harder to get wrong, unlike your sparse example,
> which has 'foo' on both branches of the conditional where you meant
> 'foo' and 'bar'.

...as several people already noticed, proving the great readability of
the sparse way of expressing oneself...


Alex



More information about the Python-list mailing list