[Python-ideas] Is this PEP-able? for X in ListY while conditionZ:
Terry Reedy
tjreedy at udel.edu
Wed Jun 26 20:16:39 CEST 2013
On 6/26/2013 12:45 AM, Nick Coghlan wrote:
> Comprehensions are currently just syntactic sugar for particular kinds
> of explicit loop, with a relatively straightforward mechanical
> translation from the expression form to the statement form. That's an
> essential property that helps keep Python's expression level and suite
> level flow control constructs from diverging into two independent
> languages that happen to share some keywords.
I think there are two points about comprehensions that people miss when
they use it as justification to proposed that their personal favorite
composition be built in to the language.
1. Comprehensions compose at least three statements, and not just two.
2. Comprehensions embody and implement a basic concept and tool of
thought -- defining a class or collection by rule, which is
complementary to defining such by roster. The former is probably more
common in everyday life and definitely so in law.
A statement form of itertools.takewhile is not in the same ballpark.
--
Displays are also 'just syntactic sugar for particular kinds of ...
loop' -- but with the loop written in the imterpreter implementation
language.
nums = [1,2,3]
is equivalent to
nums = []
nums.append(1)
nums.append(2)
nums.append(3)
and in CPython implemented as the internal equivalent of
nums = [i for i in <1,2,3>]
where <1,2,3> is a sequence of stack values. Tuples are preallocated to
their final size and filled in by index.
> I would vastly prefer implementing PEP 403 to allow the iterable in a
> comprehension to be a full generator function
I do not understand what you mean here. PEP 403 is about a new decorator
clause, whereas the iterable in comprehension can already be the return
from a generator function.
--
Terry Jan Reedy
More information about the Python-ideas
mailing list