[Python-ideas] @return?

Carl M. Johnson cmjohnson.mailinglist at gmail.com
Sun Apr 18 02:46:20 CEST 2010


On Thu, Apr 15, 2010 at 9:30 AM, Mathias Panzenböck wrote:

> filter(lt,[1,2,3,4]) where:
>        def lt(x):
>                return x < 2
>
> basically something like where in haskell. but then dis doesnt save much,
> just reverses the order of the statements (and possibly removes lt from the
> scope after filter was called). is that really enough?

If you look through the archives of python-ideas, you’ll find that
I’ve agitated for some version of this syntax several times. What I
really want is just to be able to have one or more lines written in
one order but executed in another. A where statement would do that.
Another proposal I had was


filter(@(x),[1,2,3,4]):
            return x < 2

Where “@“ meant, “function taking these parameters, to be defined in
the block to follow.”

Another proposal was to allow decorators to use arbitrarily complex
expressions. Then one could write:

@lambda f: filter(f, [1, 2, 3, 4])
def results(x):
            return x < 2

I’ve seen some other interesting stuff being done with the
with-statement today (Ex.
http://code.google.com/p/ouspg/wiki/AnonymousBlocksInPython
http://pypi.python.org/pypi/withhacks ) and proposals to make “with”
work more like an anonymous block in certain circumstances. Say

with filtering([1, 2, 3]) as result:
     def overtwo(x):
          return x < 2

(You can actually get that to work in today’s Python using withhacks,
but as the name implies, it is quite hacky, since it ends up taking
apart the stack to do its magic.)

My feeling is that until Python has some kind of block operator, these
discussions will pop up every 6 months or so. Here’s looking forward
to Fall 2010’s fruitless discussion. :-[

-- Carl M. Johnson



More information about the Python-ideas mailing list