[Python-ideas] Proposal for Ruby-style anonymous block functions (that don't kill the indention)

Carl Johnson carl at carlsensei.com
Mon Nov 10 09:07:01 CET 2008


> This bothers me.  You seem to have some kind of syntax error here,
> because you have two parens before the colon.  I don't know if you
> meant to do this, but it seems very, very strange the way you have it,
> and the obvious alternative seems strange as well:

No, the "extra" parenthesis is intentional. The original expression  
must terminate before the block begins. Not having the parenthesis  
there would mean that it needs to be added to end of the block, but  
then it wouldn't be the indentation that controls the block but the  
parenthesis, and that's unpythonic. The original idea I had was to  
have a special keyword for "variable which I will define on the next  
line, just wait a minute please" but then I simplified it down to @ on  
analogy to decorators. Still, some people might like the original idea  
more:

 >>> words = ["blah one", "Blah two", " bLAh three"]
 >>> sorted(words, key=DEFERRED)
 >>> def DEFERRED(word):
...     word = word.lower()
...     word = word.replace("one", "1")
...     word = word.replace("two", "2")
...     word = word.replace("three", "3")
...     word = word.replace(" ", "")
...     return word
...
[u'blah one', u'Blah two', u' bLAh three']

And then we just have to fight about what would be a good keyword or  
symbol for "deferred."



More information about the Python-ideas mailing list