Skip to main content

Python: lambda to the Rescue

As every Lisp programmer knows, sometimes a lambda (i.e. an anonymous, on-the-fly function) provides just the right amount of indirection you need to solve a problem elegantly. Using a lambda in Python, you can shove an expression into a function, and then call it a little later when the time is right.

I was coding some Python, and I kept ending up with code that looked like:
try:
count = d['a']['b']['c']
except KeyError:
count = 0
Translating that little idiom into a function is hard. Every single use of [] might result in a KeyError. I could use .get() on each, but that's painful. Similarly, managing a hierarchy of defaultdicts is a bit painful too; the values in my dicts are a mix of ints and other nested dicts.

The question is, how do you put the try / except part into a function, and use d['a']['b']['c'] as an argument? After all, if you call f(d['a']['b']['c']), and d['a']['b']['c'] results in a KeyError, f will never even be called.

The solution, if you haven't guessed by now, is to wrap it in a lambda. Hence, I have a function:
def default0(f):
"""Return f(), or 0 if it raises a KeyError."""
try:
return f()
except KeyError:
return 0
Here's how you use it:
default0(lambda: d['a']['b']['c'])
Viola!Voila! Nice and clean.

(Now, of course you could get really fancy with default0 such as configuring which exceptions it will catch and what value it should return. However, then I'd either have to pass those arguments or use the functools.partial function. I think default0 is simple enough, short enough, and local enough to my problem that I didn't bother.)

Comments

Fábio said…
Nice tip.

Didn't you mean "voila" instead of "viola"?

Sorry, I'm just being picky.

[]s
Kevin Smith said…
Just what I was looking for, thanks.
Bill Mill said…
This is where Haskell's "maybe" type is great. From their documentation:

"The Maybe monad embodies the strategy of combining a chain of computations that may each return Nothing by ending the chain early if any step produces Nothing as output. It is useful when a computation entails a sequence of steps that depend on one another, and in which some steps may fail to return a value."
Bill Mill said…
It should also be illegal to talk about this without referencing andand, described in detail in several raganwald posts like this one, and the excellent talk he gave about it at rubyfringe.

I'd love to see more work on this in python.
Brandon L. Golm said…
jj, I'm pretty sure you meant:

violins!
dm said…
not trying to be cute -- why would you prefer lambda to:

def dict_val(d,a,b,c):
try:
return d[a][b][c]
except:
return 0

dict_val(d,'a','b','c')

?
Jack Diederich said…
why not use a nested defaultdict?
(leading dots used for indentation)

import collections

def dds_all_the_way_down(depth, default_factory):
....if depth == 1:
........factory = default_factory
....else:
........def factory():
............return dds_all_the_way_down(depth - 1, default_factory)
....return collections.defaultdict(factory)

d = dds_all_the_way_down(3, int)

>>> print d['a']
defaultdict(function factory at 0xb7e04d84, {})
>>> print d['a']['b']
defaultdict(type 'int', {})
>>> print d['a']['b']['c']
0
jjinux said…
Those dreaded violas! They follow me wherever I go!
jjinux said…
> "The Maybe monad embodies the strategy of combining a chain of computations that may each return Nothing by ending the chain early if any step produces Nothing as output. It is useful when a computation entails a sequence of steps that depend on one another, and in which some steps may fail to return a value."

Ah, yes, I am familiar with the maybe monad. However, in this case, I wanted a 0. I wonder if there's a way to make the maybe monad do that :-/
jjinux said…
> It should also be illegal to talk about this without referencing andand,

I think andand makes more sense in Ruby where doing a hash lookup returns nil instead of raising an exception. Can you show me what andand would look like instead of default0(lambda: d['a']['b']['c'])?

Thanks for the comment!
jjinux said…
> not trying to be cute -- why would > you prefer lambda to:
>
> def dict_val(d,a,b,c):

Because sometimes I want d['a'] or d['a']['b'] or d['a']['b']['c']['d'], etc. default0 will work for anything that might return a value or raise KeyError.
jjinux said…
> why not use a nested defaultdict?
(leading dots used for indentation)

Good question. I was trying to hint at the answer in the post. I have nested dicts that mix ints and dicts. For instance:

{'http://google.com': {
'hits': 10, # int
'context': { # dict
'searching': 5,
'pinging': 7
}
}

Perhaps I could still use a defaultdict(int), but it seemed strange since I was mixing the types of the values.

Nonetheless, I still think defaultdict might be a good fit. I might try that out.
jjinux said…
I *like* defaultdict, but after trying it for a few minutes, I don't think it makes sense for my situation.

I have a tree of dicts. Each dict in the tree contains some keys that refer to ints and some keys that refer to nested dicts. I want each dict to default to an int constructor, so I can write:

d['a'] += 1

However, if they all default to int as a constructor, and if there is no d['b'], then the following can't work:

d['b']['c']['d']

It'll set d['b'] to 0, and you can't say 0['c']. In fact, I don't even want it to set d['b'] at all. If d['b'] isn't set, then I want to consider d['b']['c']['d'] to be 0 without setting it.

It was a good idea, though. Thanks.
jjinux said…
I'm thinking more about the maybe monad. Let's suppose that Python had a maybe monad. Then:

d['a']['b']['c']

would return "nothing". I still need to translate that into a 0, perhaps using a match statement. If I have to write a match statement every time I want to write d['a']['b']['c'], that's as bad as the try/finally idiom I started out with.

Perhaps I'm missing some more subtle usage of the maybe monad.

Thanks for the comment, by the way :)
dm said…
>Because sometimes I want d['a'] or
>d['a']['b'] or d['a']['b']['c']['d'],
>etc. default0 will work for anything
>that might return a value or raise
>KeyError.

take 2:


def dict_val(d,*args):
..try:
....return reduce(dict.__getitem__,args,d)
..except KeyError:
....return 0

I personally find lambda, reduce etc really confusing -- i.e. I have to slow down my scan of the code to comprehend it -- so I always tend to want to hide them away in a function or avoid altogether and use a comprehension or loop instead. Admittedly, they are powerful...
Bill Mill said…
With a maybe, wouldn't it just be "d['a']['b']['c'] or 0"? That's neater than the try: ... except, I think.

I tried to come up with a good solution with andand, but failed; it was all hairy and long-winded.

Are you wedded to python dicts? A couple simple subclasses of dict solves the problem: http://paste.pocoo.org/show/117674/
Benjamin said…
Bill, your subclasses won't work with d['a']['b']['c'] += 1 (and d['d'] += 1 and d['e']['f'] += 1...), wherein lies the problem. I've encountered this exact issue, though mine was only nested one deep and solvable easily enough via a couple if's.

I tried making a custom class that would sense whether it was being accessed as an int or a dict the first time a key was accessed by trying to write a custom __getattribute__, but that was part of my first excursion into classes in python and I ended up abandoning that attempt. I believe once I discovered the d['k'] syntax wasn't actually calling __getattribute__ and was using some other hidden heuristic to determine my still-nebulous object wasn't a dict... I gave up and just wrote a couple if's.
jjinux said…
> def dict_val(d,*args):

Interesting!

> I personally find lambda, reduce etc really confusing

"reduce" takes me a little longer to think about, but lambdas are just functions--no biggie.
jjinux said…
> With a maybe, wouldn't it just be "d['a']['b']['c'] or 0"?

I am willing to believe something with maybe could work.

> Are you wedded to python dicts? A couple simple subclasses of dict solves the problem: http://paste.pocoo.org/show/117674/

I forget the name of it, but I remember seeing a "recursive None" that would return None no matter what method you called on it. In a sense, it's like the Maybe monad.
jjinux said…
Oh, one more thing about the recursive None. You have to be careful with it. Otherwise, it can spread like gray goo: http://en.wikipedia.org/wiki/Grey_goo
jjinux said…
> I believe once I discovered the d['k'] syntax wasn't actually calling __getattribute__ and was using some other hidden heuristic to determine my still-nebulous object wasn't a dict...

d['k'] uses __getitem__.

Popular posts from this blog

Drawing Sierpinski's Triangle in Minecraft Using Python

In his keynote at PyCon, Eben Upton, the Executive Director of the Rasberry Pi Foundation, mentioned that not only has Minecraft been ported to the Rasberry Pi, but you can even control it with Python. Since four of my kids are avid Minecraft fans, I figured this might be a good time to teach them to program using Python. So I started yesterday with the goal of programming something cool for Minecraft and then showing it off at the San Francisco Python Meetup in the evening.

The first problem that I faced was that I didn't have a Rasberry Pi. You can't hack Minecraft by just installing the Minecraft client. Speaking of which, I didn't have the Minecraft client installed either ;) My kids always play it on their Nexus 7s. I found an open source Minecraft server called Bukkit that "provides the means to extend the popular Minecraft multiplayer server." Then I found a plugin called RaspberryJuice that implements a subset of the Minecraft Pi modding API for Bukkit s…

Apple: iPad and Emacs

Someone asked my boss's buddy Art Medlar if he was going to buy an iPad. He said, "I figure as soon as it runs Emacs, that will be the sign to buy." I think he was just trying to be funny, but his statement is actually fairly profound.

It's well known that submitting iPhone and iPad applications for sale on Apple's store is a huge pain--even if they're free and open source. Apple is acting as a gatekeeper for what is and isn't allowed on your device. I heard that Apple would never allow a scripting language to be installed on your iPad because it would allow end users to run code that they hadn't verified. (I don't have a reference for this, but if you do, please post it below.) Emacs is mostly written in Emacs Lisp. Per Apple's policy, I don't think it'll ever be possible to run Emacs on the iPad.

Emacs was written by Richard Stallman, and it practically defines the Free Software movement (in a manner of speaking at least). Stal…

ERNOS: Erlang Networked Operating System

I've been reading Dreaming in Code lately, and I really like it. If you're not a dreamer, you may safely skip the rest of this post ;)

In Chapter 10, "Engineers and Artists", Alan Kay, John Backus, and Jaron Lanier really got me thinking. I've also been thinking a lot about Minix 3, Erlang, and the original Lisp machine. The ideas are beginning to synthesize into something cohesive--more than just the sum of their parts.

Now, I'm sure that many of these ideas have already been envisioned within Tunes.org, LLVM, Microsoft's Singularity project, or in some other place that I haven't managed to discover or fully read, but I'm going to blog them anyway.

Rather than wax philosophical, let me just dump out some ideas:Start with Minix 3. It's a new microkernel, and it's meant for real use, unlike the original Minix. "This new OS is extremely small, with the part that runs in kernel mode under 4000 lines of executable code." I bet it&…