[Python-ideas] An alternative to PEP 572's Statement-Local Name Bindings
Nathan Schneider
neatnate at gmail.com
Sun Mar 4 23:15:07 EST 2018
On Sun, Mar 4, 2018 at 11:04 PM, Chris Angelico <rosuav at gmail.com> wrote:
> On Mon, Mar 5, 2018 at 2:49 PM, Mike Miller <python-ideas at mgmiller.net>
> wrote:
> > Yes, thanks:
> >
> > [ f(y), g(y) for x, h(x) as y in things ]
> >
> >
> > Dyslexics untie!
>
> :)
>
> Hmm. The trouble here is that a 'for' loop is basically doing
> assignment. When you say "for x, h(x) as y in things", what Python
> does is (simplified):
>
> _it = iter(things)
> while not StopIteration:
> x, (h(x) as y) = next(_it)
> ... loop body ...
>
> So what you're asking for is something that doesn't behave like an
> assignment target, but just does its own assignment. Bear in mind,
> too, that "x = next(_it)" is very different from "x, = next(_it)";
> which one is "x, (h(x) as y) = next(_it)" more like?
>
>
If I understood Mike's proposal correctly it was to allow "," to mean
'given' in this context when followed by EXPRESSION "as" VARIABLE, rather
than its usual iterable-unpacking meaning.
But I think this would cause confusion. Suppose `things` contains
pair-tuples. Then you could have
[ f(y), g(y) for x1, x2, h(x1) as y in things ]
which makes it look like (x1, x2, h(x1)) go together rather than just (x1,
x2).
Nathan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180304/43d6a5d1/attachment.html>
More information about the Python-ideas
mailing list