优雅地实现if-else(python版)

if-else版:

def if_else(operator,x,y):
    if operator=='mul':
        return x*y
    elif operator=='add':
        return x+y
    elif operator=='div':
        return x/y
    elif operator=='sub':
        return x-y
    else:
        return None

优雅版:

def dispatch_dict(operator,x,y):
    return {
        'mul':lambda :x*y,
        'add':lambda :x+y,
        'div':lambda :x/y,
        'sub':lambda :x-y
    }.get(operator,lambda :None)()