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
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=gst&q=for+else&rnum=9#1542d2041257c47e
>
> discusses the optional "else:" clause of the for statement.
>
&
[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.
>
&
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
[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
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
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
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 itera
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
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.
>
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
[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
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
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
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
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
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)
>
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
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
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
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'
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
[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")
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
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 (
27 matches
Mail list logo