For review: PEP 308 - If-then-else expression

Aahz aahz at pythoncraft.com
Sat Feb 8 09:54:07 EST 2003


In article <b22ffp$vsf$1 at slb3.atl.mindspring.net>,
Andrew Dalke <adalke at mindspring.com> wrote:
>Aahz:
>>
>> You've put your finger on the reason I feel a stronger negative reaction
>> to this than the listcomps.  It seems rare to me that someone tries to
>> directly transpose a for loop into a listcomp or vice-versa, but I think
>> that'll be much more common with this.
>
>And when that occurs, the for/if order is the same, as in
>
>result = []
>for x in data:
>  if x[0] > 5:
>    result.append(x[1])
>
>which is mapped almost directly as
>
>result = [x[1] for x in data if x[0] > 5]
>
>The term out of order is the 'append' component, not the if.

Exactly.  And because of the syntactic support provided by the brackets,
in addition to the fact that the append becomes completely implicit,
it's much clearer.
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

Register for PyCon now!  http://www.python.org/pycon/reg.html




More information about the Python-list mailing list