On Thu, Apr 12, 2018 at 4:38 AM, Ethan Furman <et...@stoneleaf.us> wrote:
> On 04/10/2018 10:32 PM, Chris Angelico wrote:
>
>> Title: Assignment Expressions
>
>
> Thank you, Chris, for doing all this!
>
> ---
>
> Personally, I'm likely to only use this feature in `if` and `while`
> statements; if the syntax was easier to read inside longer expressions then
> I might use this elsewhere -- but as has been noted by others, the
> on-the-spot assignment creates asymmetries that further clutter the overall
> expression.
>
> As Paul noted, I don't think parenthesis should be mandatory if the parser
> itself does not require them.
>
> For myself, I prefer the EXPR as NAME variant for two reasons:
>
> - puts the calculation first, which is what we are used to seeing in
> if/while statements; and
> - matches already existing expression-level assignments (context managers,
> try/except blocks)
>
> +0.5 from me.

Context managers and except blocks don't do the same thing though, so
it's a false parallel. The 'as' keyword occurs in Python's grammar
thus:

1) "with EXPR as target:" captures the return value from __enter__
2) "except EXPR as NAME:" captures the exception value, not the type(s)
3) "import NAME as NAME" loads a module object given by a token (name)
and captures that
4) "from NAME import NAME as NAME" captures an attribute of a module

Three of them use a name, one allows arbitrary assignment targets.
(You can "with spam as ham[0]:" but you can't "except Exception as
ham[0]:".) Two of them have no expressions at all, so assignment
expressions wouldn't logically be usable. The other two are
*dangerous* false parallels, because "with foo as bar:" is
semantically different from "with (foo as bar):"; it was so awkward to
try to explain that away that I actually forbade any use of "(expr as
name)" in the header of a with/except block.

The parallel is not nearly as useful as it appears to be on first
blush. The parallel with assignment statements is far closer; in fact,
many situations will behave identically whether you use the colon or
not.

> - puts the calculation first, which is what we are used to seeing in
> if/while statements; and

Not sure what you mean here; if and while statements don't have
anything BUT the calculation. A 'for' loop puts the targets before the
evaluated expression.

> - matches already existing expression-level assignments (context managers,
> try/except blocks)

They're not expression-level assignments though, or else I'm
misunderstanding something here?

ChrisA
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to