New (?) suggestion re: 'while x = f(): ...'

Aahz aahz at pythoncraft.com
Tue May 28 23:29:36 EDT 2002


In article <mailman.1022619227.16027.python-list at python.org>,
Jeff Epler  <jepler at unpythonic.net> wrote:
>
>Why not abuse the 'for' statement, since it performs an assignment to a
>user-determined name?
>
>Instead of 
>    x = v
>    if x:
>	...
>you can write
>    for x in G(v):
>	...
>
>and instead of
>    while 1:
>	x = f()
>	if not x: break
>	...
>you can write
>    for x in H(f):
>	...

This isn't abuse; this was precisely the intention of creating iterators
and generators.
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

"In the end, outside of spy agencies, people are far too trusting and
willing to help."  --Ira Winkler



More information about the Python-list mailing list