def __init__(self, test):
if not callable(test):
raise ValueError("Guard test has to be callable")
# check if test signature is ok
test_args = _getparams(test)
if len(tuple(arg for arg in test_args
if arg[1] in (p_kind.POSITIONAL_ONLY, p_kind.POSITIONAL_OR_KEYWORD))) != 1:
raise ValueError("Guard test has to have only one positional (not varying) argument")
def apply_try_conv_to_bool(function, arg):
try:
return bool(function(arg)) # test must always return boolean, so convert asap.
except TypeError: # occures when unorderable types are compared, but we don't want TypeError to be raised.
return False # couldn't compare? then guard must say no.
self.test = lambda inp: apply_try_conv_to_bool(test, inp)
function_pattern_matching.py 文件源码
python
阅读 25
收藏 0
点赞 0
评论 0
评论列表
文章目录