Ideas about optional filter on for loop statements

B

Brian L.

After following the discussion on Generator expressions on python-dev
for the last few weeks, and finally getting used to list
comprehensions as opposed to map/filter, it occurred to me that the
for loop statement lacks the filtering feature when compared to the
listcomp/genexp. For instance,

(real code)
for x in range(10):
if x%2 is 0:
print x

Could be reduced to: (fake code)

for x in range(10) if x%2 is 0:
print x

Which is comparable to iterating and printing the elements of the
listcomp:
[x for x in range(10) if x%2 is 0]

or the genexp: (this syntax isn't real yet, but I'd imagine this to be
fairly accurate based on python-dev)

(x for x in range(10) if x%2 is 0)

Note the perfect consistency between all three iterator usages.

This change would not break any compatibility that I can think of, but
would improve self-consistency within python. Furthermore, if
listcomps and genexps are a direction that python is moving in, then
it would probably make sense that the for loop statement has an
optional filtering clause as well.

The strongest argument I can think of against it is that for loop
statements should be reserved for iteration only, and that they
shouldn't have functional features (for instance, the for loop can not
and will not ever have 'map' like semantics, so why should it have
filter?). I don't think, however that this is a problem, and
considering the number of times that I've written loops in the
following patterns:

for x in l:
if x is None: continue
...

or

for x in l:
if x is not None:
...

I think that a more pythonic syntax may be in order.

for x in l if x is not None:
...

is certainly cleaner and easier to read. This clause should of course
be optional

Thoughts?

If this receives enough positive attention on comp.lang.python, then
I'll write it up and submit a PEP.

Brian
 
L

Larry Bates

Brian,

How about writing:

for x in [i for i in range(10) if i%2 is 0]:
print x

or in your other examples:

for x in [i for i in l if i]:
....

for x in [i for i in l if not i]:
....

All seem exteremely close to your proposal but
don't require any changes to current language
implementation (e.g. just use list comprehensions).

Regards,
Larry Bates
Syscon, Inc.
 
D

Duncan Booth

(e-mail address removed) (Brian L.) wrote in
for x in l if x is not None:
...

is certainly cleaner and easier to read. This clause should of course
be optional

Thoughts?

IMHO:

for x in l:
if x is not None:

is cleaner and easier to read than forcing everything onto one line. In
most cases the names or expressions would be longer (and hopefully
meaningful), so you would want to split the 'for' and 'if' onto two
separate lines anyway.

Besides, these days you can write a generator trivially, so just put the
condition inside a generator, invent a meaningful name for it and then you
don't have to return None as a value at all.
 
K

Konstantin Veretennicov

The strongest argument I can think of against it is that for loop
statements should be reserved for iteration only, and that they
shouldn't have functional features (for instance, the for loop can not
and will not ever have 'map' like semantics, so why should it have
filter?). I don't think, however that this is a problem, and
considering the number of times that I've written loops in the
following patterns:

for x in l:
if x is None: continue
...

or

for x in l:
if x is not None:
...

I think that a more pythonic syntax may be in order.

Remember how many times you've written something like this:

if condition:
return

Wouldn't it be great to have a single-line "return if condition"? ;)
But what about "... preferably only one ... obvious way to do it"?
for x in l if x is not None:
...

is certainly cleaner and easier to read.

Not sure about that. Sometimes i break list comprehensions
into several lines to make them more readable.

- kv
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top