Re: extend for loop syntax with if expr like listcomp&genexp ?
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.python archive

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

From: Ron Adam <rrr@ronadam.com>
Date: Tue Jul 12 2005 - 03:14:45 CEST

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
Received on Thu Sep 29 16:53:21 2005