In my mind, these proposed complications of the 'for' loop would *introduce* inconsistency, NOT reduce it.
It's simple to remember that suites nest statements while comprehensions are expressions on single (logical) lines. Adding more edge cases to blue the distinction makes cognitive load higher.
On Oct 3, 2016 9:38 PM, "Ken Kundert" firstname.lastname@example.org wrote:
In my experience it is exceptions and inconsistencies that consume 'working memory in the brain of humans'. By eliminating the distinction between list comprehensions and for loops we would be making the language simpler by eliminating an inconsistency.
Furthermore, I do not believe it is valid to discard a potentially good idea simply because if taken to extreme it might result in ugly code. With that justification one could reject most ideas. The fact is, that in many cases this idea would result in cleaner, more compact code. We should be content to offer a language in which it is possible to express complex ideas cleanly and simply, and trust our users to use the language appropriately.
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.
On Tue, Oct 04, 2016 at 01:31:22PM +1000, Nick Coghlan wrote:
On 4 October 2016 at 08:18, Erik email@example.com wrote:
The expression suggested could be spelled:
for i in range(10): if i != 5: body
So, if a colon followed by another suite is equivalent to the same
but without the INDENT (and then the corresponding DEDENT unwinds up
point of the first keyword) then we get something that's pretty much as succinct as Dominik suggested.
What's the pay-off though? The ultimate problem with deeply nested code isn't the amount of vertical whitespace it takes up - it's the amount of working memory it requires in the brain of a human trying to read it. "This requires a lot of lines and a lot of indentation" is just an affordance at time of writing that reminds the code author of the future readability problem they're creating for themselves.
Extracting named chunks solves the underlying readability problem by reducing the working memory demand in reading the code (assuming the chunks are well named, so the reader can either make a useful guess about the purpose of the extracted piece without even looking at its documentation, or at least remember what it does after looking it up the first time they encounter it).
By contrast, eliminating the vertical whitespace without actually reducing the level of nesting is merely hiding the readability problem without actually addressing it.
-- Nick Coghlan | firstname.lastname@example.org | Brisbane, Australia _______________________________________________ Python-ideas mailing list Pythonemail@example.com https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
Python-ideas mailing list Pythonfirstname.lastname@example.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/