python3: 'where' keyword

Steven Bethard steven.bethard at gmail.com
Fri Jan 7 13:17:39 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
> 
> or
> 
>  >>> print words[3], words[5] where:
>  >>>     words = input.split()
> 
> - defining variables in "where" block would restrict their visibility to 
> one expression

How often is this really necessary?  Could you describe some benefits of 
this?  I think the only time I've ever run into scoping problems is with 
lambda, e.g.

     [lambda x: f(x) for x, f in lst]

instead of

     [lambda x, f=f: for x, f in lst]

Are there other situations where you run into these kinds of problems?

> - it's more easy to read sources when you know which part you can skip, 
> 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.

Hmm...  This seems very heavily a matter of personal preference.  I find 
that your where clause makes me skip the 'res' assignment to read what 
the 'res' block contains.  I had to read it twice before I actually 
looked at the list comprehension.  Of course, I'm sure I could be 
retrained to read it the right way, but until I see some real benefit 
from it, I'd rather not have to.


TOOWTDI-ily-yrs,

Steve



More information about the Python-list mailing list