Created
August 23, 2017 09:39
-
-
Save geekKeen/4ad00853f3178e9802a9d5c9228bc69b to your computer and use it in GitHub Desktop.
当遇到switch-case 时 处理的技巧, 符合开放封闭原则, 以functions 中singledispath 为例
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from functions import singledispatch | |
@singledispatch | |
def my_func(args): | |
print('default myfunc({!r})'.format(arg)) | |
@my_func.register(int) | |
def my_func(args): | |
print('myfunc_int({})'.format(arg)) | |
@myfunc.register(list) | |
def myfunc_list(arg): | |
print('myfunc_list()') | |
for item in arg: | |
print(' {}'.format(item)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
dispatch
中维护一个字典,字典中存有key: func
func
;