View Single Post
Old Sep 22nd, 2005, 2:13 AM   #14
hydroxide
Programmer
 
Join Date: Apr 2005
Posts: 73
Rep Power: 4 hydroxide is on a distinguished road
Quote:
Originally Posted by Cerulean
def add(a, b):
    return a + b
Funny, I find the opposite. Care to post your benchmark?
Cerulean's way is preferable, but if you really really want wrapped dispatch:
import operator
def do_operation(op, a, b):
    return {'+': operator.add,
            '-': operator.sub,
            '*': operator.mul,
            '/': operator.div}[op](a, b)

print do_operation('+', 2, 5)
The relative difference between the speed of map() and listcomps varies depending on version - Dietrich might be running on 2.2 or 2.3 rather than 2.4?

--OH.
hydroxide is offline   Reply With Quote