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