python swich case

时间:2022-05-27 20:06:55   收藏:0   阅读:24

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")

 

原文:https://www.cnblogs.com/lshan/p/15342272.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!