Thursday, May 14, 2009

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.)

22 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

Shannon -jj Behrens said...

Those dreaded violas! They follow me wherever I go!

Shannon -jj Behrens 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 :-/

Shannon -jj Behrens 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!

Shannon -jj Behrens 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.

Shannon -jj Behrens 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.

Shannon -jj Behrens 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.

Shannon -jj Behrens 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/

Bill Mill said...

whoops, it didn't linkify it like I thought it would. Click here.

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.

Shannon -jj Behrens 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.

Shannon -jj Behrens 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.

Shannon -jj Behrens 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

Shannon -jj Behrens 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__.