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=gst&q=for+else&rnum=9#1542d2041257c47e discusses the optional "else:" clause of the for statement. I'm wondering if anyone has ever found a practical

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=gst&q=for+else&rnum=9#1542d2041257c47e > > discusses the optional "else:" clause of the for statement. > &

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=gst&q=for+else&rnum=9#1542d2041257c47e > > discusses the optional "else:" clause of the for statement. > &

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=gst&q=for+else&rnum=9#1542d2041257c47e > > > > discusses the opti

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 > els

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

2006-09-28 Thread Sion Arrowsmith
se: ... print 'else' ... else I think it's clearer to see by comparing while with if: if False: do nothing else: do something while False: do nothing else: do something and getting to the for behaviour from while is trivial. That said, I've not had much call fo

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 d

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 itera

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 t

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

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

2006-09-29 Thread BJörn Lindqvist
check its value outside the loop: You can use generator comprehension: if (i for i in list if i % 2 == 1): print "Found an odd number." else: print "No odd number found." I *think* any() should also work: if any(i % 2 == 1 in list): And so on. For ever

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

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") Unte

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

2006-09-30 Thread Sybren Stuvel
or large list): > > all_heights = (block.height for block in stack if block.is_marked()) > try: > height = all_heights.next() > height += sum(all_heights) > except StopIteration: > raise SomeError("No marked block") I must say that the for/else con

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 blo

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

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

2006-09-30 Thread Peter Otten
t;No marked block") >> >> Alternatively (lower memory usage for large list): >> >> all_heights = (block.height for block in stack if block.is_marked()) >> try: >> height = all_heights.next() >> height += sum(all_heights) >

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_emp

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

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

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'

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 m

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
s at height", height Yes, this transformation is one line longer, but the control flow is much easier to understand. In general, using the for/else clause mixes the retrieval and the usage of the element. Consider this: for item in list: if somecond(item): A) do something with i

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 (