Re:Curing Python's Neglect
by jules on Friday May 29, 2009 @07:10PM
(#633024)
Easy. The rule is that every time you see : like this:
It's the same as this:
i.e. insert `end` when the indentation ends.
[ Parent
| on HN ]
How about when the function is being passed as an argument to a function? Does the ')' just go on the next line by itself? What if it's not the last argument? There are many opportunities for ambiguity and/or ugliness.
collection.each(lambda x:
...
...
)
Or if the language doesn't require () to call a method, and uses `do` instead of `lambda`: collection.each do x:
...
...
If it's not the first argument: map(lambda x:
...
...
, collection)
Yes this is ugly, but using a multi line anonymous function in the middle of an argument list will always be ugly.in Python, it works like:
foo(a, b, lambda x: x+2)
or foo2(a, b, foo):
foo() # this is like js
The lambda is closed by the next ) or ,Right, but you can't have multi-line anonymous functions, which are pleasant to use in Ruby and JavaScript.
True, but you can just write a non-anyonymous function in the current scope, which can be passed as boo or called as boo():
def test():
def boo(x):
x += 2
x += 3
return x
something_else(boo, 33, "hi")
test()
This is equivalent to how procs are often used in Ruby...