Saturday, October 3, 2009

I Wish Python Could Support Anonymous Functions

One of the things about Python that makes me the saddest is its lack of support for multi-line lambda functions. Guido talks about the reasons for not including them here. He's right, of course: they should not be included because it would make a mess. Python's beauty lies in its simplicity, and I fully appreciate the value in not implementing special cases.

But the fact that multi-line lambdas shouldn't be added to the language doesn't imply they aren't missed, or that Python isn't worse as a language because it can't add support for them without adding special cases and making a mess.

The canonical workaround for a situation where you'd prefer to use a multi-line lambda is to use a named function nested into the same scope where you would put the lambda. Yes, it works. It's functionally equivalent. But in practice I find this additional required step limits the number of little throwaway functions I'm willing to create. The extra work of trying to create an intelligent name for the function and then having to re-locate it on all subsequent code modifications, following that extra little indirection, really slows my thinking process. It changes the kind of code I tend to write, and not in a good way.

One of the reasons I dislike Java so much is the need to come up with some Identifier for every damn thing, cluttering up the source code with completely unnecessary verbosity. I'm not a Ruby person and probably never will be, but I think its better support for anonymous functions is probably a big win for the language. I know it's a big win for JavaScript/ECMAScript.

(Incidentally, I first learned this might be a problem from Steve Yegge a few years ago when I was just getting started with Python. I'll be sad if Python dies in relative obscurity to Javascript like he seems to think it might, but that may well be the way the industry is headed.)

And yes, Python is awesome, but let's be honest about the warts.

Be the first to reply!

Post a Comment

By submitting a comment you assert that it is your own original work and agree to grant a non-exclusive licence to Brandon Thomson to display it on log.bthomson.com.