Paul Rubin enlightened us with:
height = 0
for block in stack:
if block.is_marked():
print Lowest marked block is at height, height
break
height += block.height
else:
raise SomeError(No marked block)
all_heights = [block.height for block in stack if
Sybren Stuvel wrote:
I must say that the for/else construct is a LOT more readable than the
rewritten alternatives.
+1
I just wish it had a more intuitive name like after: or then:, as
else: seems like a choice between the loop and the other block (but
really the choice is between
Sybren Stuvel [EMAIL PROTECTED] writes:
I must say that the for/else construct is a LOT more readable than the
rewritten alternatives.
They are all pretty ugly. I prefer the genexp version with a
hypothetical is_empty function:
all_heights = (block.height for block in stack if
Sybren Stuvel wrote:
Paul Rubin enlightened us with:
height = 0
for block in stack:
if block.is_marked():
print Lowest marked block is at height, height
break
height += block.height
else:
raise SomeError(No marked block)
all_heights = [block.height for
Paul Rubin wrote:
Sybren Stuvel [EMAIL PROTECTED] writes:
I must say that the for/else construct is a LOT more readable than the
rewritten alternatives.
They are all pretty ugly. I prefer the genexp version with a
hypothetical is_empty function:
all_heights = (block.height for
Peter Otten [EMAIL PROTECTED] writes:
I like
def blocks_til_mark(stack):
for block in stack:
if block.is_marked():
return
yield block
raise SomeError
height = sum(block.height for block in blocks_til_mark(stack))
Oh my, I realize now that I mis-read
Peter Otten [EMAIL PROTECTED] writes:
all_heights = (block.height for block in stack if block.is_marked())
if is_empty(all_heights):
raise SomeError(No marked block)
Such a function would have to rebind the generator:
Yeah, that's what I mean about generators not
Paul Rubin wrote:
Peter Otten [EMAIL PROTECTED] writes:
all_heights = (block.height for block in stack if
block.is_marked()) if is_empty(all_heights):
raise SomeError(No marked block)
Such a function would have to rebind the generator:
Yeah, that's what I mean about
Peter Otten [EMAIL PROTECTED] writes:
all_heights = lambda:
(block.height for block in stack if
block.is_marked())
You still need the stop() trick to omit the heights after the marked block.
Yeah, that code was based on my earlier mis-read
[EMAIL PROTECTED] wrote:
I'm wondering if anyone has ever found a practical use for the else
branch?
Say you have code that looks like this:
if command.startswith(set):
do_set_action(command)
elif command.startswith(input):
do_input_action(command)
elif command.startswith(print):
How do you transform this?
height = 0
for block in stack:
if block.is_marked():
print Lowest marked block is at height, height
break
height += block.height
else:
raise SomeError(No marked block)
def get_height_of_first_marked_bock(stack):
height = 0
for
BJörn Lindqvist wrote:
The code that you write in the positions A and B really are misplaced.
They arent part of the iteration of list. The two tasks, find item and
do something with item should be separated.
I think it is useful to have them joined. Consider a contrived example:
for i in
Actually right after posting this I came up with a great usage. I use
meld3 for my Python based dynamic HTML generation. Whenever I plan to
loop over a tree section I use a for loop, but if there is no data to
iterate over, then I simply remove that section from the tree or
populate it with a no
metaperl wrote:
Actually right after posting this I came up with a great usage. I use
meld3 for my Python based dynamic HTML generation. Whenever I plan to
loop over a tree section I use a for loop, but if there is no data to
iterate over, then I simply remove that section from the tree or
On 29 Sep 2006 11:26:10 -0700, Klaas [EMAIL PROTECTED] wrote:
else: does not trigger when there is no data on which to iterate, butwhen the loop terminated normally (ie., wasn't break-ed out).It is
meaningless without break.
The else clause *is* executed when there is no data on which to iterate.
Klaas wrote:
else: does not trigger when there is no data on which to iterate, but
when the loop terminated normally (ie., wasn't break-ed out). It is
meaningless without break.
Sorry, this was worded confusingly. else: triggers when the loop
terminates normally, not simply in the case that
On 9/29/06, Johan Steyn [EMAIL PROTECTED] wrote:
On 29 Sep 2006 11:26:10 -0700, Klaas [EMAIL PROTECTED] wrote:
else: does not trigger when there is no data on which to iterate, but
when the loop terminated normally (ie., wasn't break-ed out). It is
meaningless without break.
The else
On 9/29/06, Johan Steyn [EMAIL PROTECTED] wrote:
I agree that it is meaningless without a break statement, but I still find
it useful when I want to determine whether I looped over the whole list or
not. For example, if I want to see whether or not a list contains an odd
number:
for i in
[EMAIL PROTECTED] wrote:
And so on. For every use of the for/else clause there exists a better
alternative. Which sums up my opinion about the construct -- if you
are using it, there's something wrong with your code.
How do you transform this?
height = 0
for block in stack:
if
Matthew Woodcraft [EMAIL PROTECTED] writes:
How do you transform this?
height = 0
for block in stack:
if block.is_marked():
print Lowest marked block is at height, height
break
height += block.height
else:
raise SomeError(No marked block)
Untested:
Ben Sizer [EMAIL PROTECTED] wrote:
[EMAIL PROTECTED] wrote:
Yeah, I use it from time to time:
for foo in bar:
if foo matches some condition:
print sail to tahiti!
break
else:
print abandon ship!
As a C++ programmer (which I'm sure undermines
[EMAIL PROTECTED] wrote:
metaperl I'm wondering if anyone has ever found a practical use for the
metaperl else branch?
Yeah, I use it from time to time:
for foo in bar:
if foo matches some condition:
print sail to tahiti!
break
else:
A very old thread:
http://groups.google.com/group/comp.lang.python/browse_frm/thread/2c5022e2b7f05525/1542d2041257c47e?lnk=gstq=for+elsernum=9#1542d2041257c47e
discusses the optional else: clause of the for statement.
I'm wondering if anyone has ever found a practical use for the else
branch?
metaperl I'm wondering if anyone has ever found a practical use for the
metaperl else branch?
Yeah, I use it from time to time:
for foo in bar:
if foo matches some condition:
print sail to tahiti!
break
else:
print abandon ship!
[EMAIL PROTECTED] a écrit :
A very old thread:
http://groups.google.com/group/comp.lang.python/browse_frm/thread/2c5022e2b7f05525/1542d2041257c47e?lnk=gstq=for+elsernum=9#1542d2041257c47e
discusses the optional else: clause of the for statement.
I'm wondering if anyone has ever found a
[EMAIL PROTECTED] a écrit :
A very old thread:
http://groups.google.com/group/comp.lang.python/browse_frm/thread/2c5022e2b7f05525/1542d2041257c47e?lnk=gstq=for+elsernum=9#1542d2041257c47e
discusses the optional else: clause of the for statement.
I'm wondering if anyone has ever found a
Amaury Forgeot d'Arc wrote:
[EMAIL PROTECTED] a écrit :
A very old thread:
http://groups.google.com/group/comp.lang.python/browse_frm/thread/2c5022e2b7f05525/1542d2041257c47e?lnk=gstq=for+elsernum=9#1542d2041257c47e
discusses the optional else: clause of the for statement.
I'm
27 matches
Mail list logo