:-$

Ryan's work blog

My Links

News

The WeatherPixie
Subscribe with Bloglines
About this blog

Tools I use:

Post Categories

Article Categories

Archives

Image Galleries

Blog Stats

Personal

Projects

Random Blogs

Random other

Reference

Web comics

Work

Python's lambda considered completely useless?

We're starting to use Python in earnest now, for some prime development purposes, and I'm looking into usages of the lambda keyword. In short, lambda is there to let you define anonymous functions, basically a shorthand for making little helpers to simplify your code. So, you can say something like:
>>>def doStuff():
      sumSquares = lambda x,y: x*x + y*y
      print sumSquares(1,1)
>>>doStuff()
2
The problem is I want my helper functions to do more than return a value, and Python doesn't think I should be able to do that. Specifically, I want to write a little helper to union two dictionaries. I wanted to do this:
>>>def addAttributes(h1, h2):
      append = lambda k,v: h1[k] = v
      [append(k,v) for k,v in h2.items()]
Unfortunately, I "can't assign to lambda". That might be for the best, as a singular "addAttribute" function isn't a bad thing to add, but I don't like a language telling me "No" unless it's Prolog. Naturally, Nathan is quick to point out that this can be done easily in Lisp.

posted on Monday, January 03, 2005 3:46 PM