[python]代码库
def cmds_dispatcher():
commands = {}
# 注册
def reg(name):
def _reg(fn):
commands[name] = fn
return fn
return _reg
def defaultfunc():
print("Unkown command")
def dispatcher():
while True:
cmd = input('>>')
if cmd.strip() == 'quit':
return
commands.get(cmd,defaultfunc)()
return reg,dispatcher
reg,dispatcher = cmds_dispatcher()
# r,d = cmds_dispatcher()
# 自定义函数 注册
@reg('mag')
def foo1():
print('welcome magedu')
@reg('py') #foo2 = reg('py')(foo2)
def foo2():
print('welcome python')
dispatcher()
初级程序员
by: 云代码会员 发表于:2021-06-21 22:30:30 顶(0) | 踩(0) 回复
饿
回复评论