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

Terry Hancock hancock at anansispaceworks.com
Wed Jul 13 00:07:07 EDT 2005


On Monday 11 July 2005 08:53 pm, Bengt Richter wrote:
> On Tue, 12 Jul 2005 10:12:33 +1000, John Machin <sjmachin at lexicon.net> wrote:
> >Bengt Richter wrote:
> >>     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):

Seems like a bug waiting to happen -- wouldn't someone using that
idiom most likely have *meant* something like this:

for i,x in enumerate(seq):
	if x is not None:
		print "seq[%d] = %s is not None" % (i, repr(x))

?

But of course that's not equivalent.  It's hard to imagine a
use case for an enumerated loop when the object being
iterated over is anonymous (will be lost as soon as the loop
exits).

--
Terry Hancock ( hancock at anansispaceworks.com )
Anansi Spaceworks  http://www.anansispaceworks.com




More information about the Python-list mailing list