[Python-ideas] Introducing where clauses (original) (raw)

Andrey Popp 8mayday at gmail.com
Mon Jun 22 12:16:05 CEST 2009


Hello.

Reading discussion on python-ideas about "Accessing the result of comprehension's expression from the conditional", I've came to the idea of where clauses, similar to Haskell's.

This solves the problem of recalculating of value multiple times. For example, in the following expression:

[(f(x), f(x)) for x in some_iterable if f(x) < 2]

value f(y) calculates three times -- It is a problem if function f takes much time to compute its value or has side effects. If we would have where clause, we can rewrite expression above with:

[(y, y) for x in some_iterable if y < 2 where y = f(x)]

I think it is really useful. We can also expand this idea to lambdas or maybe to introducing arbitrary scoping blocks of code.

Other thoughts:



More information about the Python-ideas mailing list