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

B

Bengt Richter

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
 
J

John Machin

Bengt said:
E.g., so we could write

for x in seq if x is not None:

Chundrous; looks like that p**l language ...
print repr(x), "isn't None ;-)"

instead of

for x in (x for x in seq if x is not None):

Byzantine ...
print repr(x), "isn't None ;-)"

just a thought.

What's wrong with the following?

for x in seq:
if x is not None:
print repr(x), "isn't None ;-)"
 
R

Ron Adam

Bengt said:
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
 
B

Bengt Richter

Chundrous; looks like that p**l language ...
^^^^^^^^^--piqued my interest, where'd that come from? ;-)
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):
What's wrong with the following?

for x in seq:
if x is not None:
print repr(x), "isn't None ;-)"

Nothing. Just noting that there's (at least) two kinds of for --
the plain old one, and the ones inside list comprehensions and generator
expressions, and it struck me that not allowing the full listcomp/genexp
syntax in the ordinary for context was a seemingly unnecessary restriction.

Regards,
Bengt Richter
 
T

Terry Hancock

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).
 
B

Bengt Richter

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).
Line numbers in a listing of non-None things?
Page breaks at the right places?
Filtering out '' instead of NOne from results of a string split before creating numbered
html names for links to non-blank text elements in rendering text as html?
I dunno, seems like at least a few possibilities for something halfway sensible...

Regards,
Bengt Richter
 
P

Paul Rubin

Terry Hancock said:
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).

Huh? Not at all.

print 'List of Python fans:'
for i,x in enumerate([p for p in people if p.favorite_language == 'Python']):
print '%d. %s'% (i, x.name)
 

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