Sep 19th, 2005, 1:36 PM
|
#11
|
|
Expert Programmer
Join Date: Aug 2005
Location: UK
Posts: 862
Rep Power: 4 
|
Quote:
|
Originally Posted by Dietrich
How would you replace lambda in dictionary usage?
# Python can do a switch/case like thingy using a dictionary and lambda
# basis for a nice calculator
def do_operation(op, a, b):
"""here a dictionary combined with the lambda inline function acts like switch/case"""
return {'+': lambda: a + b,
'-': lambda: a - b,
'*': lambda: a * b,
'/': lambda: a / b
}[op]()
print do_operation('+', 2, 5)
|
That is a nice bit of coding  GJ
__________________
Join us at #programmingforums @ irc.freenode.net!
My software never has bugs. It just develops random features.
|
|
|