检查两个对象是否相互比较,而不依赖于引发的错误

Check if two objects are comparable to each other, without relying on raised errors

"comparable" 是指 "able to mutually perform the comparison operations >, <, >=, <=, ==, and != without raising a TypeError"。这个 属性 确实适用于许多不同的 类:

1 < 2.5  # int and float
2 < decimal.Decimal(4)  # int and Decimal
"alice" < "bob"  # str and str
(1, 2) < (3, 4)  # tuple and tuple

它没有:

1 < "2"  # int and str
1.5 < "2.5"  # float and str

即使看起来确实应该:

datetime.date(2018, 9, 25) < datetime.datetime(2019, 1, 31)  # date and datetime 
[1, 2] < (3, 4)  # list and tuple

,您显然可以使用传统的 python 方法 "ask forgiveness, not permission" 和 ab 检查两个未知类型的对象使用 try/except 块:

try: 
    a < b
    # do something
except TypeError:
    # do something else

但是 catching exceptions is expensive,我希望第二个分支能够足够频繁地进行,所以我想在 if/else 语句中捕捉到这一点反而。我该怎么做?

由于在您实际执行比较操作之前不可能事先知道是否可以对两种特定类型的操作数执行此类操作,因此您可以做的最接近于实现避免必须捕获的期望行为的行为TypeError是缓存已知的运算符组合和之前已经造成TypeError的左右操作数的类型。您可以通过使用此类缓存和包装器方法创建一个 class 来执行此操作,这些缓存和包装器方法在进行比较之前进行此类验证:

from operator import gt, lt, ge, le

def validate_operation(op):
    def wrapper(cls, a, b):
        # the signature can also be just (type(a), type(b)) if you don't care about op
        signature = op, type(a), type(b)
        if signature not in cls.incomparables:
            try:
                return op(a, b)
            except TypeError:
                cls.incomparables.add(signature)
        else:
            print('Exception avoided for {}'.format(signature)) # for debug only
    return wrapper

class compare:
    incomparables = set()

for op in gt, lt, ge, le:
    setattr(compare, op.__name__, classmethod(validate_operation(op)))

这样:

import datetime
print(compare.gt(1, 2.0))
print(compare.gt(1, "a"))
print(compare.gt(2, 'b'))
print(compare.lt(datetime.date(2018, 9, 25), datetime.datetime(2019, 1, 31)))
print(compare.lt(datetime.date(2019, 9, 25), datetime.datetime(2020, 1, 31)))

会输出:

False
None
Exception avoided for (<built-in function gt>, <class 'int'>, <class 'str'>)
None
None
Exception avoided for (<built-in function lt>, <class 'datetime.date'>, <class 'datetime.datetime'>)
None

这样您就可以使用 if 语句而不是异常处理程序来验证比较:

result = compare.gt(obj1, obj2)
if result is None:
    # handle the fact that we cannot perform the > operation on obj1 and obj2
elsif result:
    # obj1 is greater than obj2
else:
    # obj1 is not greater than obj2

下面是一些时间统计数据:

from timeit import timeit
print(timeit('''try:
    1 > 1
except TypeError:
    pass''', globals=globals()))
print(timeit('''try:
    1 > "a"
except TypeError:
    pass''', globals=globals()))
print(timeit('compare.gt(1, "a")', globals=globals()))

在我的机器上输出:

0.047088712933431365
0.7171912713398885
0.46406612257995117

如您所见,缓存比较验证确实在比较抛出异常时为您节省了大约 1/3 的时间,但在没有抛出异常时慢了大约 10 倍,因此这种缓存机制只有在以下情况下才有意义您预计绝大多数比较都会引发异常。

你可以做的是在比较之前使用isinstance,然后自己处理异常。

if(isinstance(date_1,datetime) != isinstance(date_2,datetime)):
#deal with the exception