Proposed new syntax

Steve D'Aprano steve+python at pearwood.info
Fri Aug 11 01:45:28 EDT 2017


On Fri, 11 Aug 2017 08:49 am, Ben Finney wrote:

> The comprehension encourages thinking in sets: an operation that takes a
> collection as input, and emits a different collection, through one
> conceptual operation.
> 
> Adding ‘while’ in there encourages thinking not in terms of a single
> set-based operation, but an iteration of separate operations. That
> confuses the model, and I no longer have a coherent model about which to
> reason what the syntax might mean.


Sorry Ben, you've completely lost me.

If you had said that tradition functional style operations such as:

map(func, iterable)

filter(pred, iterable)

"encourages thinking in sets: an operation that takes a collection as input, and
emits a different collection, through one conceptual operation"

then I would completely agree with you. I agree that is absolutely true:
traditional functional programming idioms encourage thinking of looping as a
single conceptual operation. For simplicity, both map() and filter() are often
implemented as a for loop that operates on one item at a time, in order, but
conceptually the map and filter could operate in parallel on all items at once.

But that's not the case for list comprehensions and generator expressions (which
use almost exactly the same syntax). The sequential, one-item-at-a-time nature
isn't a mere implementation detail, it is an essential part of the semantics of
the comprehension.

Comprehension syntax makes the sequential loop explicit: the loop is right there
in the syntax:

[expr for x in iterable]

It makes no sense to me to argue that this explicit looping construct with a
defined order is conceptually "set based". The comprehension:

[print(i) for i in (1, 2, 3)]

better print the values 1, 2, 3 *in that order* or there is a serious problem.
The same is not true of map(), at least not conceptually.



>> For comparison, what would you expect this to return? (Without
>> actually trying it, thank you.)
>>
>> [x + 1 for x in (0, 1, 2, 999, 3, 4) if x < 5]
> 
> Though ‘for’ is used elsewhere in Python to mean iteration, ‘for’ also
> has strong connotation in mathematics for set-based operations (“the
> result is foo for all bar, if baz”). So the same confusion doesn't
> occur: this is a comprehension which is about set-based thinking, which
> is supported by all the semantic connotations of the syntax.

I don't understand this. I *think* what you're saying is "I have internalised
the explicit for-loop and think of it as a filter plus a map and no longer
think of it as a for-loop", but I'm not sure.


> So I think ‘while’ should not be added to the syntax of comprehension
> expressions. I agree with those who think it is not obvious what it
> would mean.


-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list