I find it very useful if I am allowed to define new local variables in
a list comprehension. For example, I wish to have something like
[(x, y) for x in range(10) for y := x ** 2 if x + y < 80], or
[(x, y) for x in range(10) with y := x ** 2 if x + y < 80].
For now this functionality can be achieved by writing
[(x, y) for x in range(10) for y in [x ** 2] if x + y < 80].
Is it worthwhile to add a new feature like this in Python? If so, how
can I propose this to PEP?
Hello,
I find it very useful if I am allowed to define new local variables in a list comprehension. For example, I wish to have something like
[(x, y) for x in range(10) for y := x ** 2 if x + y < 80], or
[(x, y) for x in range(10) with y := x ** 2 if x + y < 80].
For now this functionality can be achieved by writing
[(x, y) for x in range(10) for y in [x ** 2] if x + y < 80].
Is it worthwhile to add a new feature like this in Python? If so, how can I propose this to PEP?
I find it very useful if I am allowed to define new local variables in a
list comprehension. For example, I wish to have something like
[(x, y) for x in range(10) for y := x ** 2 if x + y < 80], or
[(x, y) for x in range(10) with y := x ** 2 if x + y < 80].
For now this functionality can be achieved by writing
[(x, y) for x in range(10) for y in [x ** 2] if x + y < 80].
在 2022年9月1日星期四 UTC+2 18:34:36,<ery...@gmail.com> 写道:
On 9/1/22, James Tsai <james...@gmail.com> wrote:
I find it very useful if I am allowed to define new local variables in a list comprehension. For example, I wish to have something like
[(x, y) for x in range(10) for y := x ** 2 if x + y < 80], or
[(x, y) for x in range(10) with y := x ** 2 if x + y < 80].
For now this functionality can be achieved by writingYou can assign a local variable in the `if` expression. For example:
[(x, y) for x in range(10) for y in [x ** 2] if x + y < 80].
[(0, 0), (1, 1), (2, 4), (3, 9), (4, 16)][(x, y) for x in range(10) if x + (y := x**2) < 30]
Yeah this works great but like [(x, y) for x in range(10) for y in [x**2]] I written before, is kind of a hack. And if initially I do not need an "if" condition in the list comprehension, this becomes less convenient. I still can write
[(x, y) for x in range(10) if (y := x**2) or True]
But I wonder if Python could have a specific syntax to support this.
No but very often when I have written a neat list/dict/set comprehension, I find it very necessary
to define local variable(s) to make it more clear and concise. Otherwise I have to break it down
to several incrementally indented lines of for loops, if statements, and variable assignments,
which I think look less nice.
On 9/1/22, James Tsai <james...@gmail.com> wrote:
I find it very useful if I am allowed to define new local variables in a list comprehension. For example, I wish to have something like
[(x, y) for x in range(10) for y := x ** 2 if x + y < 80], or
[(x, y) for x in range(10) with y := x ** 2 if x + y < 80].
For now this functionality can be achieved by writingYou can assign a local variable in the `if` expression. For example:
[(x, y) for x in range(10) for y in [x ** 2] if x + y < 80].
[(0, 0), (1, 1), (2, 4), (3, 9), (4, 16)][(x, y) for x in range(10) if x + (y := x**2) < 30]
[(x, y) for x in range(10) if (y := x**2) or True]
On Fri, 2 Sept 2022 at 02:10, James Tsai <james...@gmail.com> wrote:
Hello,
I find it very useful if I am allowed to define new local variables in a list comprehension. For example, I wish to have something like
[(x, y) for x in range(10) for y := x ** 2 if x + y < 80], or
[(x, y) for x in range(10) with y := x ** 2 if x + y < 80].
For now this functionality can be achieved by writing
[(x, y) for x in range(10) for y in [x ** 2] if x + y < 80].
Is it worthwhile to add a new feature like this in Python? If so, how can I propose this to PEP?Not everything has to be a one-liner.
ChrisA
James Tsai <james...@gmail.com> writes:
I find it very useful if I am allowed to define new local variables in
a list comprehension. For example, I wish to have something like
[(x, y) for x in range(10) for y := x ** 2 if x + y < 80], or
[(x, y) for x in range(10) with y := x ** 2 if x + y < 80].
For now this functionality can be achieved by writingx and y are, to a first approximation, new local variables defined in a
[(x, y) for x in range(10) for y in [x ** 2] if x + y < 80].
list comprehension. I think you need to restate what it is you want.
Is it worthwhile to add a new feature like this in Python? If so, howTo make any sort of case you'd need to give an example that does not
can I propose this to PEP?
have a clearer way to write it already. Your working version is, to me, clearer that the ones you want to be able to write.
--
Ben.
在 2022年9月1日星期四 UTC+2 18:34:36,<ery...@gmail.com> 写道:
On 9/1/22, James Tsai <james...@gmail.com> wrote:
I find it very useful if I am allowed to define new local variables in a list comprehension. For example, I wish to have something like
[(x, y) for x in range(10) for y := x ** 2 if x + y < 80], or
[(x, y) for x in range(10) with y := x ** 2 if x + y < 80].
For now this functionality can be achieved by writingYou can assign a local variable in the `if` expression. For example:
[(x, y) for x in range(10) for y in [x ** 2] if x + y < 80].
[(0, 0), (1, 1), (2, 4), (3, 9), (4, 16)][(x, y) for x in range(10) if x + (y := x**2) < 30]
Yeah this works great but like [(x, y) for x in range(10) for y in
[x**2]] I written before, is kind of a hack. And if initially I do not
need an "if" condition in the list comprehension, this becomes less convenient. I still can write
[(x, y) for x in range(10) if (y := x**2) or True]
On Fri, 2 Sept 2022 at 06:55, James Tsai <jamestztsai@gmail.com> wrote:
No but very often when I have written a neat list/dict/set
comprehension, I find it very necessary
to define local variable(s) to make it more clear and concise. Otherwise I have to break it down
to several incrementally indented lines of for loops, if statements, and variable assignments,
which I think look less nice.
Well, if it's outgrown a list comp, write it on multiple lines. Like I
said, not everything has to be a one-liner.
On Fri, 2 Sept 2022 at 06:55, James Tsai <jamestztsai@gmail.com> wrote:
在 2022年9月1日星期四 UTC+2 18:34:36,<ery...@gmail.com> 写道:
On 9/1/22, James Tsai <james...@gmail.com> wrote:
in aI find it very useful if I am allowed to define new local variables
list comprehension. For example, I wish to have something like
[(x, y) for x in range(10) for y := x ** 2 if x + y < 80], or
[(x, y) for x in range(10) with y := x ** 2 if x + y < 80].
For now this functionality can be achieved by writingYou can assign a local variable in the `if` expression. For example:
[(x, y) for x in range(10) for y in [x ** 2] if x + y < 80].
[(0, 0), (1, 1), (2, 4), (3, 9), (4, 16)][(x, y) for x in range(10) if x + (y := x**2) < 30]
Yeah this works great but like [(x, y) for x in range(10) for y in[x**2]] I written before, is kind of a hack. And if initially I do not need an "if" condition in the list comprehension, this becomes less convenient.
I still can write
[(x, y) for x in range(10) if (y := x**2) or True]
But I wonder if Python could have a specific syntax to support this.
But why would you need to assign to y in that example? If you're using
it more than once, you can use :=, and if you aren't, you don't need
to. But do be aware that := does not create a comprehension-local name binding, but a nonlocal instead.
No but very often when I have written a neat list/dict/setcomprehension, I find it very necessary
to define local variable(s) to make it more clear and concise. OtherwiseI have to break it down
to several incrementally indented lines of for loops, if statements, andvariable assignments,
which I think look less nice.
Well, if it's outgrown a list comp, write it on multiple lines. Like I
said, not everything has to be a one-liner.
ChrisA
--
https://mail.python.org/mailman/listinfo/python-list
On 02Sep2022 07:01, Chris Angelico <ros...@gmail.com> wrote:
On Fri, 2 Sept 2022 at 06:55, James Tsai <james...@gmail.com> wrote:
No but very often when I have written a neat list/dict/set
comprehension, I find it very necessary
to define local variable(s) to make it more clear and concise. Otherwise I have to break it down
to several incrementally indented lines of for loops, if statements, and variable assignments,
which I think look less nice.
Well, if it's outgrown a list comp, write it on multiple lines. Like I >said, not everything has to be a one-liner.True, but a comprehension can be more expressive than a less
"functional" expression (series of statements).
James, can you provide (a) a real world example where you needed to
write a series of statements or loops and (b) a corresponding example of
how you would have preferred to have written that code, possibly
inventing some syntax or misusing ":=" as if it workeed they way you'd
like it to work?
Cheers,
Cameron Simpson <c...@cskk.id.au>
On Fri, 2 Sept 2022 at 02:10, James Tsai <jamestztsai@gmail.com> wrote:
Hello,
I find it very useful if I am allowed to define new local variables in alist comprehension. For example, I wish to have something like
[(x, y) for x in range(10) for y := x ** 2 if x + y < 80], or
[(x, y) for x in range(10) with y := x ** 2 if x + y < 80].
For now this functionality can be achieved by writing
[(x, y) for x in range(10) for y in [x ** 2] if x + y < 80].
Is it worthwhile to add a new feature like this in Python? If so, howcan I propose this to PEP?
Not everything has to be a one-liner.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 300 |
Nodes: | 16 (2 / 14) |
Uptime: | 64:36:55 |
Calls: | 6,712 |
Files: | 12,244 |
Messages: | 5,356,126 |