46 points · by tav · 17y ago · 42 comments
If you want to get this past python-dev you'll need to explain the benefits, here's a start:
1) Lower cognitive load: Users can understand blocks but the extra naming/reference is one too many things for their brain stack to handle.
2) More descriptive: with blocks the first line says "I'm about to define a function for use with X" instead of the existing way which says "I'm defining a function. Now I'm using that function with X."
3) Show me: take a chunk of an existing project (stdlib is best) and rewrite it with the proposed block syntax. You get to cherry pick the example so if the new version doesn't read much cleaner than the old version maybe the idea isn't so hot.
I'm -1 on the idea but if you want to change my mind you have to make an argument that doesn't start with "In Ruby..."
Besides, it's not the Python style to have function calls without the () syntax.
I think you could do something interesting with decorators, though.
Sorry about the url. Tinyurl couldnt digest it. :-)
I think 'there' could have good traction, but nobody has made a PEP (see python.org). If a proposal doesnt have a champion, it isn't going anywhere.
I'd happily swap that syntax for some of the other features modern Python has grown, like the "with" statement, or decorators.
Anyway, I'm kind of sad that people seem to want a straight copy of Ruby's blocks. I'd prefer syntax that allowed passing more than one block to a function, like the C-like syntax I proposed here: http://www.hackerdashery.com/2006/10/code-blocks-and-c-like-...
map(foo, pairs) where:
pairs = [(0,1), (20, 10), (30, 10)]
def foo(pair):
a = pair[1] * 2
b = pair[0] * a
return (b, a)
I'm not terribly familiar with Python's grammar, so it might be difficult to introduce a closing where-clause without ambiguity. In that case, it might be useful to use a do clause as well: do:
<statements>
where:
<statements>
Another alternative would be to simply create a real function literal syntax (i.e., lambda that doesn't suck).Once you understand that, a lot of the "issues" go away.
####### def throwaway_function(emp): if emp.salary > developer.salary: return fireEmployee(emp) else: return extendContract(emp)
employees.select(throwaway_function) ######
Python lambda: filter(lambda emp: fireEmployee(emp) if emp.salary > developer.salary else extendContract(emp), employees)
Don't know what select does but it sounds like filter. So if that works as intended(is the ruby version both returning value and side-effecting?) or not I'm not sure but you probably could make it.
No other language does that, because it makes very little sense. This proposal will make Python behave more like every other programming language that supports anonymous functions.
Having thought about it for a couple of hours, I don't think there is any good solution for adding multi-line anonymous functions to Python. The suggested syntax feels non-Pythonic, is inherently unexpressive and difficult to read, only useful in a few select use-cases while not solving the holy grail of 'pleasant anonymous functions', and the single problem it's solving isn't common enough or big enough to need dedicated syntax and could already largely be solved with a named function. The fact it enables 'misuse' via Ruby-style iteration, going against Pythonic ideals of 'one way to do things', doesn't work in its favour either.
I don't think analogies with decorators are good either. The advantages of decorators is they transform something otherwise difficult-to-read and unexpressive into something easier-to-read and more expressive, whereas this makes something that's easy-to-read less expressive with the only advantage being terseness. Python, unlike other languages, focuses primarily on clarity over code length.
Haskell is newline-and-indentation-based and allows multi-line anonymous functions.
With that in mind, I often prefer to name functions, but not let them be globally callable, i.e.
f = (g . h) where
g = (2*)
h = (1+)
Does Python allow something like this? (Equivalents in other languages: (defun f (x)
(flet ((g (x) (* 2 x))
(h (x) (1+ x)))
(g (h x))))
Or: sub f {
my $g = sub { 2 * $_[0] };
my $h = sub { 1 + $_[0] };
return $g->($h->($_[0]));
}
I think Haskell is prettiest, but this is not a concept unique to Haskell, and it's a technique I find useful in all three languges.)Having anonymous functions only for the situation where you are passing exactly one of them to another function and it is the last argument is much more arbitrary than the current style of always temporarily binding functions to names.
Read some Ruby code that uses blocks for an example of how passing anonymous functions can become a very clear and natural idiom. In Ruby, you can write your own simple control flow constructs in the form of functions that take a block -- in fact the idiomatic way to iterate through a collection in Ruby is with a function rather than a built-in control flow construct.
In languages like Lisp and Smalltalk, where functions can naturally take more than one in-line block, you can write any control flow construct yourself.
myfunction( lambda a: a.foo > a.bar )
I think the following looks neater: aname = lambda a: a.foo > a.bar
myfunction( aname )
I think that putting unnecessary clutter inside of a function call is just asking for readability problems.