函数可以通过其参数来调度
arg-dispatch的Python项目详细描述
arg_调度
函数可以通过其参数进行调度
示例
fromarg_dispatchimportdispatch# Functions@dispatchdefdemo(a,b):return'hello'@dispatchdefdemo(c):return'world'demo(a=1,b=2)# return 'hello'demo(c=3)# return 'world'# try to call a function which has not been registeddemo(d=4)# raise `FunctionNotRegist`# MethodsclassDemo(object):@dispatchdefdemo(self,a,b):return'hello'@dispatchdefdemo(self,c):return'world'instance=Demo()instance.demo(a=1,b=2)# return 'hello'instance.demo(c=3)# return 'world'# try to call a method which has not been registedinstance.demo(d=4)# raise `FunctionNotRegist`
注意
位置参数必须是必需的
demo(1,2)# Boom!?, raise `ArgumentError`instance.demo(1,2)# Boom!?, raise `ArgumentError`
也不支持默认值
@dispatchdefdemo(a,b=1):# Boom!?, raise `ExistDefaultValue`return'hello'classDemo(object):@dispatchdefdemo(self,a,b=1):# Boom!?, raise `ExistDefaultValue`return'hello'