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

Ron Adam rrr at ronadam.com
Mon Jul 11 21:14:45 EDT 2005


Bengt Richter wrote:
> E.g., so we could write
> 
>     for x in seq if x is not None:
>         print repr(x), "isn't None ;-)"
> 
> instead of
> 
>     for x in (x for x in seq if x is not None):
>         print repr(x), "isn't None ;-)"
> 
> just a thought.
> 
> Regards,
> Bengt Richter

Is it new idea month?  :)



That would seem to follow the pattern of combining sequential lines that 
end in ':'.


if pay<10 if hours>10 if stressed:
    sys.exit()

That would be the same as using ands.



And this gives us an if-try pattern with a shared else clause.

if trapped try:
    exit = find('door')
except:
    yell_for_help()
else:              #works for both if and try!  ;-D
    leave()


Which would be the same as:

if trapped:
    try:
       exit = find('door')
    except:
       yell_for_help()
    else:
       leave()
else:
    leave()


Interesting idea, but I think it might make reading other peoples code 
more difficult.


Cheers,
Ron



More information about the Python-list mailing list