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

Weeble clockworksaint at gmail.com
Wed Jul 15 12:45:05 CEST 2009


> Date: Wed, 15 Jul 2009 19:55:15 +1200
> From: Greg Ewing <greg.ewing at canterbury.ac.nz>
> To: python-ideas at python.org
> Subject: [Python-ideas] Where-statement (Proposal for function
>        expressions)
> Message-ID: <4A5D8B63.4040509 at canterbury.ac.nz>
> Content-Type: text/plain; charset=UTF-8; format=flowed
>
> Carl Johnson wrote:
>
>> My suggestion for future would be language extenders is that they try
>> to solve a broader problem than just the "how do I make callbacks more
>> convenient" problem.
>
> One such extension might be a "where" block. Applied
> to the current problem:
>
>   foo(f) where:
>     def f(x):
>       ...

I was just thinking of something similar:

in:
    foo(f,a)
let:
    def f(x):
        ...
    a = ...

(Keywords open to debate.) It's a little verbose, but I dislike it
much less than all the proposals that introduce special sigils or do
weird things to the syntax of the code. Things I like about it:
there's a clear warning (the "in:") right at the start that the
statement (or block?) is special and relies on code that comes after;
the syntax isn't complex; both lets you provide a name and prevents it
leaking into places you don't want it; and the contents of both the
"in:" block and the "let:" block are normal Python. I'd expect the
behaviour to be something like this: execute the "let:" block in its
own nested scope, then execute the "in:" block in the regular local
scope but with the symbols defined by the "let:" block available. Does
that make some sort of sense?

Most of the things I like are achieved by Greg's "where:" block, the
only thing I don't like about it is tacking a keyword onto the end of
a statement, which feels a bit awkward to me. That may just be
aesthetics. I certainly like the "where:" idea a lot better than the
other proposals. It seems to achieve the most utility with the least
confusing syntax.



More information about the Python-ideas mailing list