extend for loop syntax with if expr like listcomp&genexp ?

Bengt Richter bokr at oz.net
Mon Jul 11 21:53:27 EDT 2005


On Tue, 12 Jul 2005 10:12:33 +1000, John Machin <sjmachin at lexicon.net> wrote:

>Bengt Richter wrote:
>> E.g., so we could write
>> 
>>     for x in seq if x is not None:
>
>Chundrous; looks like that p**l language ...
 ^^^^^^^^^--piqued my interest, where'd that come from? ;-)

>
>>         print repr(x), "isn't None ;-)"
>> 
>> instead of
>> 
>>     for x in (x for x in seq if x is not None):
>
>Byzantine ...
Perhaps not if you wanted to enumerate the selected elements, as in
       for i, x in enumerate(x for x in seq if x is not None):
>
>>         print repr(x), "isn't None ;-)"
>> 
>> just a thought.
>> 
>
>What's wrong with the following?
>
>for x in seq:
>     if x is not None:
>         print repr(x), "isn't None ;-)"

Nothing. Just noting that there's (at least) two kinds of for --
the plain old one, and the ones inside list comprehensions and generator
expressions, and it struck me that not allowing the full listcomp/genexp
syntax in the ordinary for context was a seemingly unnecessary restriction.

Regards,
Bengt Richter



More information about the Python-list mailing list