function_pattern_matching.py 文件源码

python
阅读 25 收藏 0 点赞 0 评论 0

项目:function-pattern-matching 作者: rasguanabana 项目源码 文件源码
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)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号