Re: [Python-ideas] An alternative to PEP 572's Statement-Local Name Bindings

2018-03-03 8:40 GMT+01:00 Nick Coghlan <ncoghlan@gmail.com>:
pairs = [(f(y), g(y)) for x in things with bind(h(x)) as y]
I don't mucn like "with bind(h(x)) as y" because it's kind of like an abstraction inversion -- you're building something complicated on top of something complicated in order to get something simple, instead of just having the simple thing to begin with. If nothing else, it has a huge runtime cost for the benefit it gives. -- Greg

On 2018-03-03 16:51, Greg Ewing wrote:
Reading this thread I was thinking that the assignment part was happening too far away from where the action was and came up with this variant: [ f(y), g(y) for x, y as h(x) in things ] Plus or minus an extra set of parentheses for clarity. -Mike

On Mon, Mar 5, 2018 at 2:39 PM, Mike Miller <python-ideas@mgmiller.net> wrote:
Did you mean: [ f(y), g(y) for x, h(x) as y in things ] ? Elsewhere in Python, "a as b" takes "a" and binds it to the name "b". Otherwise, I'm not sure what you meant. ChrisA

Yes, thanks: [ f(y), g(y) for x, h(x) as y in things ] Dyslexics untie! On 2018-03-04 19:45, Chris Angelico wrote:

On Mon, Mar 5, 2018 at 2:49 PM, Mike Miller <python-ideas@mgmiller.net> wrote:
:) 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? ChrisA

On Sun, Mar 4, 2018 at 11:04 PM, Chris Angelico <rosuav@gmail.com> wrote:
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

On 2018-03-03 16:51, Greg Ewing wrote:
Reading this thread I was thinking that the assignment part was happening too far away from where the action was and came up with this variant: [ f(y), g(y) for x, y as h(x) in things ] Plus or minus an extra set of parentheses for clarity. -Mike

On Mon, Mar 5, 2018 at 2:39 PM, Mike Miller <python-ideas@mgmiller.net> wrote:
Did you mean: [ f(y), g(y) for x, h(x) as y in things ] ? Elsewhere in Python, "a as b" takes "a" and binds it to the name "b". Otherwise, I'm not sure what you meant. ChrisA

Yes, thanks: [ f(y), g(y) for x, h(x) as y in things ] Dyslexics untie! On 2018-03-04 19:45, Chris Angelico wrote:

On Mon, Mar 5, 2018 at 2:49 PM, Mike Miller <python-ideas@mgmiller.net> wrote:
:) 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? ChrisA

On Sun, Mar 4, 2018 at 11:04 PM, Chris Angelico <rosuav@gmail.com> wrote:
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
participants (4)
-
Chris Angelico
-
Greg Ewing
-
Mike Miller
-
Nathan Schneider