[Python-ideas] Where-statement (Proposal for function expressions)

Daniel Stutzbach daniel at stutzbachenterprises.com
Wed Jul 15 14:49:36 CEST 2009


On Wed, Jul 15, 2009 at 2:55 AM, Greg Ewing <greg.ewing at canterbury.ac.nz>wrote:

> One such extension might be a "where" block. Applied
> to the current problem:
>
>  foo(f) where:
>    def f(x):
>      ...
>

I like this proposal much more than any of the previous proposals.  It has
some of the flavor of Lisp's "let", but puts the important line first
instead of at the end.  In a nutshell, it says "define these symbols for
this one line only".  On the downside, for the common case of wanting to
define a single function, the body of the function must be indented twice.

I suggest the following grammar and meaning, which would be referred to by
the assignment, yield, return, and expression statements:

where_expression ::= expression_list "where" ":" suite | expression_list


It evaluates as follows.

[return|yield|x=] expression_list where:
   suite

is roughly equivalent to:

def where_expression():
    suite
    return expression_list
[return|yield|x=] expression_list()


How about the following as additional syntactic sugar for the common
one-function case?

x = blah(f) where def f(item):
   body_of_f

--
Daniel Stutzbach, Ph.D.
President, Stutzbach Enterprises, LLC <http://stutzbachenterprises.com/>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20090715/cb93c799/attachment.html>


More information about the Python-ideas mailing list