View Single Post
Old Sep 19th, 2005, 1:36 PM   #11
coldDeath
Expert Programmer
 
coldDeath's Avatar
 
Join Date: Aug 2005
Location: UK
Posts: 862
Rep Power: 4 coldDeath is on a distinguished road
Send a message via AIM to coldDeath Send a message via Yahoo to coldDeath
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.
coldDeath is offline   Reply With Quote