PEP 572: Create a section on alternate proposals
This commit is contained in:
parent
b2d428ce48
commit
2b4ca20963
47
pep-0572.rst
47
pep-0572.rst
|
@ -135,6 +135,53 @@ Open questions
|
|||
adding to the confusion.
|
||||
|
||||
|
||||
Alternative syntax proposals
|
||||
============================
|
||||
|
||||
Proposals of this nature have come up frequently on python-ideas. Below are
|
||||
a number of alternative syntaxes, some of them specific to comprehensions,
|
||||
which have been rejected in favour of the one given above.
|
||||
|
||||
1. ``where``, ``let``, ``given``::
|
||||
|
||||
stuff = [(y, y) where y = f(x) for x in range(5)]
|
||||
stuff = [(y, y) let y = f(x) for x in range(5)]
|
||||
stuff = [(y, y) given y = f(x) for x in range(5)]
|
||||
|
||||
This brings the subexpression to a location in between the 'for' loop and
|
||||
the expression. It introduces an additional language keyword, which creates
|
||||
conflicts. Of the three, ``where`` reads the most cleanly, but also has the
|
||||
greatest potential for conflict (eg SQLAlchemy and numpy have ``where``
|
||||
methods, as does ``tkinter.dnd.Icon`` in the standard library).
|
||||
|
||||
2. ``with``::
|
||||
|
||||
stuff = [(y, y) with y = f(x) for x in range(5)]
|
||||
|
||||
As above, but reusing the `with` keyword. Doesn't read too badly, and needs
|
||||
no additional language keyword. Is restricted to comprehensions, though,
|
||||
and cannot as easily be transformed into "longhand" for-loop syntax. Has
|
||||
the C problem that an equals sign in an expression can now create a name
|
||||
binding, rather than performing a comparison.
|
||||
|
||||
3. ``with... as``::
|
||||
|
||||
stuff = [(y, y) with f(x) as y for x in range(5)]
|
||||
|
||||
As per option 2, but using ``as`` in place of the equals sign. Aligns
|
||||
syntactically with other uses of ``as`` for name binding, but a simple
|
||||
transformation to for-loop longhand would create drastically different
|
||||
semantics; the meaning of ``with`` inside a comprehension would be
|
||||
completely different from the meaning as a stand-alone statement.
|
||||
|
||||
4. ``EXPR as NAME`` without parentheses::
|
||||
|
||||
stuff = [[f(x) as y, y] for x in range(5)]
|
||||
|
||||
Omitting the parentheses from this PEP's proposed syntax introduces many
|
||||
syntactic ambiguities.
|
||||
|
||||
|
||||
References
|
||||
==========
|
||||
|
||||
|
|
Loading…
Reference in New Issue