python3: 'where' keyword

AdSR adsr at
Fri Jan 7 15:04:28 EST 2005

Andrey Tatarinov wrote:
> Hi.
> It would be great to be able to reverse usage/definition parts in 
> haskell-way with "where" keyword. Since Python 3 would miss lambda, that 
> would be extremly useful for creating readable sources.
> Usage could be something like:
>  >>> res = [ f(i) for i in objects ] where:
>  >>>     def f(x):
>  >>>         #do something

I don't know haskell, but it looks SQL-ish to me (only by loose 
association). And it's not that unpythonic - it resembles

 >>> res = [x for x in sequence if x.isOk()]

> or
>  >>> print words[3], words[5] where:
>  >>>     words = input.split()

Here's a shorter version:

 >>> print input.split()[3:5:2]

(Does it qualify as obfuscated Python code? :) )

> - defining variables in "where" block would restrict their visibility to 
> one expression
> - it's more easy to read sources when you know which part you can skip,

Yes, I like the readability of it, too.

> compare to
>  >>> def f(x):
>  >>>     #do something
>  >>> res = [ f(i) for i in objects ]
> in this case you read definition of "f" before you know something about 
> it usage.

When I first read your post, I thought "Well, just one more of those 
Py3k ideas that appear on every day." But as I look at the latter 
example, I think you have just scratched my itch. The same thing has 
bugged me more than once in my code.

I think this idea is of the same kind as the @decorator syntax. Guido 
moved an operation to a point in the code where it was more visible. You 
moved an operation to a more local context where (pun not intended) it 
really belongs.

I'm usually rather conservative about Python syntax (@decorators, 
absolute/relative imports, if-else operator), but this one could appear 
in Python tomorrow and that would be too far in the future for me ;)



More information about the Python-list mailing list