On 4 October 2016 at 14:32, Ken Kundert admin@shalmirane.com wrote:
For example, it was suggested that one could simplify a multi-level loop by moving the multiple levels of for loop into a separate function that acts as generator. And that is a nice idea, but when writing it, the writing of the generator function represents a speed bump. Whereas writing something like the following is simple, compact, quick, and obvious. There is no reason why it should not be allowed even though it might not always be the best approach to use:
for i in range(5) for j in range(5) for k in range(5): ...
And I would really like to be able to write loops of the form:
for item in items if item is not None: ...
It is something I do all the time, and it would be nice if it did not consume two levels on indentation.
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.
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:
On 4 October 2016 at 14:32, Ken Kundert admin@shalmirane.com wrote:
For example, it was suggested that one could simplify a multi-level loop
by
moving the multiple levels of for loop into a separate function that
acts as
generator. And that is a nice idea, but when writing it, the writing of
the
generator function represents a speed bump. Whereas writing something
like the
following is simple, compact, quick, and obvious. There is no reason why
it
should not be allowed even though it might not always be the best
approach to
use:
for i in range(5) for j in range(5) for k in range(5): ...
And I would really like to be able to write loops of the form:
for item in items if item is not None: ...
It is something I do all the time, and it would be nice if it did not
consume
two levels on indentation.
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 _______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
On Tue, Oct 4, 2016, at 07:37, Nick Coghlan 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?
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:
On Tue, Oct 4, 2016, at 07:37, Nick Coghlan 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?
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).
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)
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]
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.
On 04.10.2016 15:20, Random832 wrote:
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]
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 Oct 4, 2016 6:20 AM, "Random832" random832@fastmail.com wrote:
for item in items if item is not None: ... else: # ???
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).
I think it's obvious it would be the innermost construct... Or at least very plausible.
On Wed, Oct 5, 2016 at 2:42 AM, David Mertz mertz@gnosis.cx wrote:
On Oct 4, 2016 6:20 AM, "Random832" random832@fastmail.com wrote:
for item in items if item is not None: ... else: # ???
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).
I think it's obvious it would be the innermost construct... Or at least very plausible.
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