470,648 Members | 1,396 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,648 developers. It's quick & easy.

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


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
Jul 21 '05 #1
6 1383
Bengt Richter wrote:
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 ;-)"
Jul 21 '05 #2
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
Jul 21 '05 #3
On Tue, 12 Jul 2005 10:12:33 +1000, John Machin <sj******@lexicon.net> wrote:
Bengt Richter wrote:
E.g., so we could write

for x in seq if x is not None:
Chundrous; looks like that p**l language ...

^^^^^^^^^--piqued my interest, where'd that come from? ;-)
print repr(x), "isn't None ;-)"

instead of

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):
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 ;-)"


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
Jul 21 '05 #4
On Monday 11 July 2005 08:53 pm, Bengt Richter wrote:
On Tue, 12 Jul 2005 10:12:33 +1000, John Machin <sj******@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

Jul 21 '05 #5
On Tue, 12 Jul 2005 23:07:07 -0500, Terry Hancock <ha*****@anansispaceworks.com> wrote:
On Monday 11 July 2005 08:53 pm, Bengt Richter wrote:
On Tue, 12 Jul 2005 10:12:33 +1000, John Machin <sj******@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).

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
Jul 21 '05 #6
Terry Hancock <ha*****@anansispaceworks.com> writes:
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)
Jul 21 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

16 posts views Thread by George Sakkis | last post: by
19 posts views Thread by Nicolas Fleury | last post: by
2 posts views Thread by Adrienne Boswell | last post: by
11 posts views Thread by Rene | last post: by
reply views Thread by Mark Dickinson | last post: by
ddtpmyra
2 posts views Thread by ddtpmyra | last post: by
1 post views Thread by Korara | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.