Re: Simple if-else question

2009-10-01 Thread Sion Arrowsmith
MRAB pyt...@mrabarnett.plus.com wrote: [ for ... else ] The example that makes it clearest for me is searching through a list for a certain item and breaking out of the 'for' loop if I find it. If I get to the end of the list and still haven't broken out then I haven't found the item, and that's

Re: Simple if-else question

2009-10-01 Thread Olof Bjarnason
2009/10/1 Sion Arrowsmith s...@viridian.paintbox: MRAB  pyt...@mrabarnett.plus.com wrote: [ for ... else ] The example that makes it clearest for me is searching through a list for a certain item and breaking out of the 'for' loop if I find it. If I get to the end of the list and still haven't

Re: Simple if-else question

2009-09-30 Thread Steven D'Aprano
On Tue, 29 Sep 2009 22:29:10 -0700, John Yeung wrote: On Sep 29, 1:15 pm, Carl Banks pavlovevide...@gmail.com wrote: Hmm, I wonder if Python should emit a warning if an else is used on a for block with no break inside.  I don't think the else can be invoked in any other way.  As a bonus it

Re: Simple if-else question

2009-09-30 Thread Terry Reedy
John Yeung wrote: On Sep 29, 1:15 pm, Carl Banks pavlovevide...@gmail.com wrote: Hmm, I wonder if Python should emit a warning if an else is used on a for block with no break inside. I don't think the else can be invoked in any other way. As a bonus it could catch some cases where people

Re: Simple if-else question

2009-09-30 Thread dksr
On Sep 29, 6:38 pm, Duncan Booth duncan.bo...@invalid.invalid wrote: Carl Banks pavlovevide...@gmail.com wrote: Hmm, I wonder if Python should emit a warning if an else is used on a for block with no break inside.  I don't think the else can be invoked in any other way.  As a bonus it could

Re: Simple if-else question

2009-09-30 Thread Iain King
On Sep 30, 7:12 am, Steven D'Aprano ste...@remove.this.cybersource.com.au wrote: On Tue, 29 Sep 2009 22:29:10 -0700, John Yeung wrote: On Sep 29, 1:15 pm, Carl Banks pavlovevide...@gmail.com wrote: Hmm, I wonder if Python should emit a warning if an else is used on a for block with no break

Re: Simple if-else question

2009-09-30 Thread Duncan Booth
Iain King iaink...@gmail.com wrote: However, I assume you can get past the else by raising an exception, so the idea becomes a little muddled - do you warn when there is no break and no explicit raise caught outside the loop? What about an implicit exception? I would guess that code

Re: Simple if-else question

2009-09-30 Thread Carl Banks
On Sep 30, 3:40 am, Iain King iaink...@gmail.com wrote: Read the suggestion again - it's not a warning on the for-else structure, it's a warning when the for-else doesn't contain a break; he's theorising that a for-else without a break will always trigger the else, in which case it's almost

Re: Simple if-else question

2009-09-30 Thread alex23
dksr dksre...@gmail.com wrote: Yes thats what I thought. for-else looks similar to if-else and in if- else, else part is executed only when if part is not executed, but in for-else it has entirely a different job. If you think of if-else more in terms of the else-branch occurring when the

Simple if-else question

2009-09-29 Thread Sandy
Hi all, A simple and silly if-else question. I saw some code that has the following structure. My question is why else is used there though removing else has the same result. More important, is it not syntactically wrong :-( for i in xrange(8): if i 4: print i else: print i

Re: Simple if-else question

2009-09-29 Thread Joel Goldstick
Sandy wrote: Hi all, A simple and silly if-else question. I saw some code that has the following structure. My question is why else is used there though removing else has the same result. More important, is it not syntactically wrong :-( for i in xrange(8): if i 4: print i else:

Re: Simple if-else question

2009-09-29 Thread Gary Herron
Sandy wrote: Hi all, A simple and silly if-else question. I saw some code that has the following structure. My question is why else is used there though removing else has the same result. More important, is it not syntactically wrong :-( for i in xrange(8): if i 4: print i else:

Re: Simple if-else question

2009-09-29 Thread Ethan Furman
Sandy wrote: Hi all, A simple and silly if-else question. I saw some code that has the following structure. My question is why else is used there though removing else has the same result. More important, is it not syntactically wrong :-( for i in xrange(8): if i 4: print i else:

Re: Simple if-else question

2009-09-29 Thread Chris Kaynor
If I'm reading the indentation correctly, the else is applying to the for loop, not the if statement. When used in this way, the else occurs only if the for loop exits due to completion (aka, the for loop does not exit due to a break or return statement). I would expect the output from that

Re: Simple if-else question

2009-09-29 Thread Carl Banks
On Sep 29, 9:08 am, Gary Herron gher...@islandtraining.com wrote: Sandy wrote: Hi all, A simple and silly if-else question. I saw some code that has the following structure. My question is why else is used there though removing else has the same result. More important, is it not

Re: Simple if-else question

2009-09-29 Thread MRAB
Ethan Furman wrote: Sandy wrote: Hi all, A simple and silly if-else question. I saw some code that has the following structure. My question is why else is used there though removing else has the same result. More important, is it not syntactically wrong :-( for i in xrange(8): if i 4:

Re: Simple if-else question

2009-09-29 Thread Duncan Booth
Carl Banks pavlovevide...@gmail.com wrote: Hmm, I wonder if Python should emit a warning if an else is used on a for block with no break inside. I don't think the else can be invoked in any other way. As a bonus it could catch some cases where people mistakenly use it thinking it will

Re: Simple if-else question

2009-09-29 Thread Carl Banks
On Sep 29, 10:38 am, Duncan Booth duncan.bo...@invalid.invalid wrote: Carl Banks pavlovevide...@gmail.com wrote: Hmm, I wonder if Python should emit a warning if an else is used on a for block with no break inside.  I don't think the else can be invoked in any other way.  As a bonus it

Re: Simple if-else question

2009-09-29 Thread John Yeung
On Sep 29, 1:15 pm, Carl Banks pavlovevide...@gmail.com wrote: Hmm, I wonder if Python should emit a warning if an else is used on a for block with no break inside.  I don't think the else can be invoked in any other way.  As a bonus it could catch some cases where people mistakenly use it

Re: Simple if-else question

2009-09-29 Thread John Yeung
On Sep 29, 1:25 pm, MRAB pyt...@mrabarnett.plus.com wrote: The example that makes it clearest for me is searching through a list for a certain item and breaking out of the 'for' loop if I find it. If I get to the end of the list and still haven't broken out then I haven't found the item, and