python3: 'where' keyword

Donn Cave donn at u.washington.edu
Fri Jan 7 20:53:37 CET 2005


In article <xdCdnUz9muXfT0PcRVn-vg at comcast.com>,
 Steven Bethard <steven.bethard at gmail.com> wrote:
> Andrey Tatarinov wrote:

> > 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?

Note that he says "would be extremely useful for creating readable
sources", so the "these kinds of problems" he would have been thinking
of would be where source was not as readable as it could be.  You seem
to be concerned about something else.

I don't by any means agree that this notation is worth adopting, and
in general I think this kind of readability issue is more or less a lost 
cause for a language with Python's scoping rules, but the motive makes
sense to me.  One way to look at it might be, if I observe that "words"
is assigned to in a where clause, then I  know it will not be used
elsewhere in the surrounding scope so I can forget about it right away.
If the name does occur elsewhere, it evidently refers to something else.

   Donn Cave, donn at u.washington.edu



More information about the Python-list mailing list