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:
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: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.)
I was coding some Python, and I kept ending up with code that looked like:
try: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.
count = d['a']['b']['c']
except KeyError:
count = 0
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):Here's how you use it:
"""Return f(), or 0 if it raises a KeyError."""
try:
return f()
except KeyError:
return 0
default0(lambda: d['a']['b']['c'])
(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
Didn't you mean "voila" instead of "viola"?
Sorry, I'm just being picky.
[]s
"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."
I'd love to see more work on this in python.
violins!
def dict_val(d,a,b,c):
try:
return d[a][b][c]
except:
return 0
dict_val(d,'a','b','c')
?
(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
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 :-/
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!
>
> 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.
(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.
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.
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 :)
>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...
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/
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.
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.
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.
d['k'] uses __getitem__.