Re: for: else: - any practical uses for the else clause?

2006-09-30 Thread Sybren Stuvel
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

Re: for: else: - any practical uses for the else clause?

2006-09-30 Thread MonkeeSage
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

Re: for: else: - any practical uses for the else clause?

2006-09-30 Thread Paul Rubin
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

Re: for: else: - any practical uses for the else clause?

2006-09-30 Thread Peter Otten
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

Re: for: else: - any practical uses for the else clause?

2006-09-30 Thread Peter Otten
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

Re: for: else: - any practical uses for the else clause?

2006-09-30 Thread Paul Rubin
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

Re: for: else: - any practical uses for the else clause?

2006-09-30 Thread Paul Rubin
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

Re: for: else: - any practical uses for the else clause?

2006-09-30 Thread Peter Otten
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

Re: for: else: - any practical uses for the else clause?

2006-09-30 Thread Paul Rubin
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

Re: for: else: - any practical uses for the else clause?

2006-09-30 Thread Carl Banks
[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):

Re: for: else: - any practical uses for the else clause?

2006-09-30 Thread BJörn Lindqvist
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

Re: for: else: - any practical uses for the else clause?

2006-09-30 Thread MonkeeSage
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

Re: for: else: - any practical uses for the else clause?

2006-09-29 Thread metaperl
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

Re: for: else: - any practical uses for the else clause?

2006-09-29 Thread Klaas
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

Re: for: else: - any practical uses for the else clause?

2006-09-29 Thread Johan Steyn
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.

Re: for: else: - any practical uses for the else clause?

2006-09-29 Thread Klaas
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

Re: for: else: - any practical uses for the else clause?

2006-09-29 Thread Mike Klaas
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

Re: for: else: - any practical uses for the else clause?

2006-09-29 Thread BJörn Lindqvist
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

Re: for: else: - any practical uses for the else clause?

2006-09-29 Thread Matthew Woodcraft
[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

Re: for: else: - any practical uses for the else clause?

2006-09-29 Thread Paul Rubin
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:

Re: for: else: - any practical uses for the else clause?

2006-09-28 Thread Sion Arrowsmith
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

Re: for: else: - any practical uses for the else clause?

2006-09-27 Thread Ben Sizer
[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:

for: else: - any practical uses for the else clause?

2006-09-26 Thread metaperl . etc
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?

Re: for: else: - any practical uses for the else clause?

2006-09-26 Thread skip
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!

Re: for: else: - any practical uses for the else clause?

2006-09-26 Thread Amaury Forgeot d'Arc
[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

Re: for: else: - any practical uses for the else clause?

2006-09-26 Thread Bruno Desthuilliers
[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

Re: for: else: - any practical uses for the else clause?

2006-09-26 Thread Fuzzyman
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