Re: [Python-ideas] if-statement in for-loop

On 4 October 2016 at 14:32, Ken Kundert <admin@shalmirane.com> wrote:
And when you add the "else" clause that's supported by both "for" and "if", what does that mean in the abbreviated form? for item in items if item is not None: ... else: # ??? Or is the implicit proposal that this form be special cased to disallow the "else" clause? Comprehensions don't have that concern, as they don't support "else" clauses at all. Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia

Something else that may look confusing can be a break statement; in a for i in range(5) for j in range(5) for k in range(5): ... break does it break the inner "k" loop, going to the next "j" (as it would happen with 3 nested loops), or does it end the whole for statement? Similar question with "continue" On 4 October 2016 at 12:37, Nick Coghlan <ncoghlan@gmail.com> wrote:
-- Daniel F. Moisset - UK Country Manager www.machinalis.com Skype: @dmoisset

On Tue, Oct 4, 2016, at 07:37, Nick Coghlan wrote:
I think it's obvious that it would be on the outermost construct (i.e. the one that would still be at the same indentation level fully expanded). The *real* question is what "break" should do. I think it should likewise break from the outermost for-loop (but "continue" should still continue the innermost one), but this does mean that it's not mechanically identical to the "equivalent" nested loops [it would, however, make it mechanically identical to the "generator and single loop" form]

On 4 October 2016 at 23:20, Random832 <random832@fastmail.com> wrote:
But would that interpretation be obvious to folks that aren't yet aware that you can have "else" clauses on loops? (Folks can be *years* into using Python before they first encounter that, whether in real code or in a "Did you know <this> about Python?" snippet)
Or we could stick with the status quo where limiting the keyword chaining to the expression form naturally avoids all of these awkward interactions with other statement level constructs. Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia

On 04.10.2016 15:20, Random832 wrote:
To me, a for loop starts with a "for" and ends with a ":". I wouldn't mind the ability of more "for"s or "if"s in between. I would skip over them anyway while reading. Technically, I agree with you as it matches my intuition: for blaa foo blah blaaaa blubber babble: break # go outside continue # go to next item else: # no break Cheers, Sven

On Wed, Oct 5, 2016 at 2:42 AM, David Mertz <mertz@gnosis.cx> wrote:
My reading of this is that the loop consists of a single filtered iteration, ergo break/continue/else are as if the loop used a generator: # for item in items if item is not None: for item in (item for item in items if item is not None): These two would be semantically equivalent, and the first one has the advantage of not sounding like the Cheshire Cat as Alice entered 'Machinations'. << Time to jump in time to jump through time.... I'm dizzy. >> ChrisA

Something else that may look confusing can be a break statement; in a for i in range(5) for j in range(5) for k in range(5): ... break does it break the inner "k" loop, going to the next "j" (as it would happen with 3 nested loops), or does it end the whole for statement? Similar question with "continue" On 4 October 2016 at 12:37, Nick Coghlan <ncoghlan@gmail.com> wrote:
-- Daniel F. Moisset - UK Country Manager www.machinalis.com Skype: @dmoisset

On Tue, Oct 4, 2016, at 07:37, Nick Coghlan wrote:
I think it's obvious that it would be on the outermost construct (i.e. the one that would still be at the same indentation level fully expanded). The *real* question is what "break" should do. I think it should likewise break from the outermost for-loop (but "continue" should still continue the innermost one), but this does mean that it's not mechanically identical to the "equivalent" nested loops [it would, however, make it mechanically identical to the "generator and single loop" form]

On 4 October 2016 at 23:20, Random832 <random832@fastmail.com> wrote:
But would that interpretation be obvious to folks that aren't yet aware that you can have "else" clauses on loops? (Folks can be *years* into using Python before they first encounter that, whether in real code or in a "Did you know <this> about Python?" snippet)
Or we could stick with the status quo where limiting the keyword chaining to the expression form naturally avoids all of these awkward interactions with other statement level constructs. Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia

On 04.10.2016 15:20, Random832 wrote:
To me, a for loop starts with a "for" and ends with a ":". I wouldn't mind the ability of more "for"s or "if"s in between. I would skip over them anyway while reading. Technically, I agree with you as it matches my intuition: for blaa foo blah blaaaa blubber babble: break # go outside continue # go to next item else: # no break Cheers, Sven

On Wed, Oct 5, 2016 at 2:42 AM, David Mertz <mertz@gnosis.cx> wrote:
My reading of this is that the loop consists of a single filtered iteration, ergo break/continue/else are as if the loop used a generator: # for item in items if item is not None: for item in (item for item in items if item is not None): These two would be semantically equivalent, and the first one has the advantage of not sounding like the Cheshire Cat as Alice entered 'Machinations'. << Time to jump in time to jump through time.... I'm dizzy. >> ChrisA
participants (6)
-
Chris Angelico
-
Daniel Moisset
-
David Mertz
-
Nick Coghlan
-
Random832
-
Sven R. Kunze