Tuesday, October 7, 2008

A bit of a case

Saw an interesting talk tonight on PyMeta which brought back memories of one of my old coworkers working on natural language processing. It also gave me some insight into how a compiler works. Neither of those points are related to what I wanted to jot down though. In showing us some code, the speaker (Tom Marsell) revealed a case-like statement. It struck me as sweet so I jotted it down...partially. When I got home, here is what I got to work:

def meth(operator, num1, num2):
return {
'+': lambda: num1 + num2,
'-': lambda: num1 - num2
}[operator]()

print meth('+', 2, 2)

I don't think that's what Tom had up there but it inspired me nonetheless. Thanks Tom.

No comments: