python swich case,使用字典实现,原理,闭包

# swich case
def handler_result(op, ns, value):
    todos = {
        "i": insert,
        "u": update,
        "d": delete
    }
    method = todos.get(op, other)
    if method:
        method(ns, value)

def other(ns, value):
    print("ns:"+ns+" v:"+value)
    print("other")
def update(ns, value):
    print("ns:"+ns+" v:"+value)
    print("update")
def insert(ns, value):
    print("ns:"+ns+" v:"+value)
    print("insert")
def delete(ns, value):
    print("ns:"+ns+" v:"+value)
    print("delete")

if __name__ == '__main__':
    # start_kafkaListener()
    handler_result("d", "sea.test", "xxxdel")
    handler_result("i", "sea.test", "xxxinsert")
    handler_result("u", "sea.test", "xxxupdate")

 

posted on 2021-09-27 11:44  lshan  阅读(38)  评论(0编辑  收藏  举报