51 lines
964 B
Python
51 lines
964 B
Python
def func_dispatch(func):
|
|
registry = {}
|
|
|
|
def dispatch(key_word):
|
|
return registry.get(key_word, registry[object])
|
|
|
|
def register(key_word, func=None):
|
|
if func is None:
|
|
return lambda f: register(key_word, f)
|
|
|
|
registry[key_word] = func
|
|
return func
|
|
|
|
def wrapper(*args, **kw):
|
|
return dispatch(args[0])(*args, **kw)
|
|
|
|
registry[object] = func
|
|
wrapper.register = register
|
|
return wrapper
|
|
|
|
|
|
@func_dispatch
|
|
def score_dispath(course):
|
|
return 0
|
|
|
|
|
|
@score_dispath.register('mathematical')
|
|
def get_mathematical_score(course):
|
|
return 90
|
|
|
|
|
|
@score_dispath.register('english')
|
|
def get_english_score(course):
|
|
return 95
|
|
|
|
|
|
@score_dispath.register('history')
|
|
def get_history_score(course):
|
|
return 98
|
|
|
|
|
|
def get_score_by_course(course):
|
|
"""
|
|
根据课程获取考试分数
|
|
:param course:
|
|
:return:
|
|
"""
|
|
return score_dispath(course)
|
|
|
|
|
|
print(get_score_by_course('mathematical')) |