
Hey all. Ever had some list comprehension hell in your code? Me neither *whistles 418 happly*... I was thinking about this idea and while `this` keyword is equalevant to `self` i have to explain myself. English is not my main language, sorry for that :' ) Here is my pseudo code. ``` if [i for i in range(10) if i == 11]: print(this) Evaluate: [] ``` Another one ``` if [i for i in range(10) if i == 5]: print(this) Evaluate: [5] ``` As I try to show above. It would be neat to make a list comprhension if statement and use those results in the if condition as the `this` parameter Instead of declaring variables like ``` a = [i for i in range(10) if i == 5] if a: print(a) Evaluate: [5] ``` I hope I explained my idea well enough and hope to see something like this in the future. If anyone has questions on my interpretation please ask.

*available since Python 3.8. Link here: https://docs.python.org/3/whatsnew/3.8.html On Mon, 15 Jun 2020 at 16:06, Henk-Jaap Wagenaar <wagenaarhenkjaap@gmail.com> wrote:

On Mon, Jun 15, 2020 at 03:01:55PM +0000, M Bfmv wrote:
Hey all. Ever had some list comprehension hell in your code? Me neither *whistles 418 happly*...
If you are having list comprehension hell, you are doing too much in list comprehensions. They are a hammer. Not everything is a nail.
I was thinking about this idea and while `this` keyword is equalevant to `self` i have to explain myself.
I use "this" as a variable in some of my code. If you make it a keyword, you will break my code. "self" is not a heyword, it is an ordinary variable.
You don't need a special keyword for that. Just assign it to a variable. this = [i for i in range(10) if i == 11] if this: print(this) What's wrong with this solution? You mention it at the end of your message, as if it was something to be avoided. Or in 3.8 and after: if this := [i for i in range(10) if i == 11]: print(this) -- Steven

M Bfmv writes:
This usage of 'this' in that code is called "anaphora". It's very useful in natural language and most (all? :-) natural languages have it, but in a programming language it requires a convention (that must be learned and remembered by anyone who reads your code, including last year's lint programs!) and either requires reserving the conventional identifier (in your case, 'this') or creating ambiguity when some programmer uses it for something else. Some programming languages have it. Python has preferred to avoid it. The assignment operator (aka "walrus operator") that others have pointed out is a good compromise IMO: if this := [i for i in range(10) if i == 11]: print(this) Of course the walrus operator is very new and has the same burden on human readers and linters, but it's far more flexible and useful than the implicit use of anaphora. I think the addition of the walrus operator means there will be no anaphora in Python ever. Steve

*available since Python 3.8. Link here: https://docs.python.org/3/whatsnew/3.8.html On Mon, 15 Jun 2020 at 16:06, Henk-Jaap Wagenaar <wagenaarhenkjaap@gmail.com> wrote:

On Mon, Jun 15, 2020 at 03:01:55PM +0000, M Bfmv wrote:
Hey all. Ever had some list comprehension hell in your code? Me neither *whistles 418 happly*...
If you are having list comprehension hell, you are doing too much in list comprehensions. They are a hammer. Not everything is a nail.
I was thinking about this idea and while `this` keyword is equalevant to `self` i have to explain myself.
I use "this" as a variable in some of my code. If you make it a keyword, you will break my code. "self" is not a heyword, it is an ordinary variable.
You don't need a special keyword for that. Just assign it to a variable. this = [i for i in range(10) if i == 11] if this: print(this) What's wrong with this solution? You mention it at the end of your message, as if it was something to be avoided. Or in 3.8 and after: if this := [i for i in range(10) if i == 11]: print(this) -- Steven

M Bfmv writes:
This usage of 'this' in that code is called "anaphora". It's very useful in natural language and most (all? :-) natural languages have it, but in a programming language it requires a convention (that must be learned and remembered by anyone who reads your code, including last year's lint programs!) and either requires reserving the conventional identifier (in your case, 'this') or creating ambiguity when some programmer uses it for something else. Some programming languages have it. Python has preferred to avoid it. The assignment operator (aka "walrus operator") that others have pointed out is a good compromise IMO: if this := [i for i in range(10) if i == 11]: print(this) Of course the walrus operator is very new and has the same burden on human readers and linters, but it's far more flexible and useful than the implicit use of anaphora. I think the addition of the walrus operator means there will be no anaphora in Python ever. Steve
participants (5)
-
Henk-Jaap Wagenaar
-
M Bfmv
-
Rob Cliffe
-
Stephen J. Turnbull
-
Steven D'Aprano