Has the where clause, as used in Haskell, ever been explored for Python List comprehension?
For example,
text_numbers = [ "one", "two", "three", .... ]
# Print pair numbers
print(
[
x for x in text_numbers
if num % 2 == 0
where num = get_number_from_text(x)
]
)
We could define variables in the list comprehension scope in a legible manner using the ‘where’ clause.
The example should be equal to:
text_numbers = [ "one", "two", "three", .... ]
# Print pair numbers
for x in text_numbers:
num = get_number_from_text(x) # where clause
if num % 2 == 0:
print( x )
That may be true, though I will point out something you are probably already aware of:
% python -m this | grep 'should be one'
There should be one-- and preferably only one --obvious way to do it.
The problem with doing something in multiple, similar, ways is that the language gets harder to read. You are coming from a Haskell background. Many of the rest of us aren’t, so your proposted construct looks foreign to (some of) us.
Let me flip the script. I have been working my way through The Rust Programming Language. I find myself wondering, “Why aren’t they calling it ‘X’ not ‘Y’?” where X and Y are some language constructs. I realize I’m reading the book with my Python rose-colored glasses firmly perched on my nose.
Another exaple. The first time I wanted to extend a long expression across multiple lines I wrote:
if (some-rust-expression &&
some-other-rust-expression) {
... execute-some-rusty-stuff ...
}
the compiler warned me about the superfluous parens. Oh, okay, that’s not accepted practice in Rust, where it would be the preferred way to do things in Python. So be it. I could leave them and nothing would break, but rustc would complain every time it recompiled my code.