我正在浏览six.py
django utils 中的代码,对于非 Jython 实现,它会尝试找到 int 的 MAXSIZE。现在,这样做的方式很有趣——不是在语句本身上捕获异常,而是将语句包装在__len__
自定义类的方法中。这样做的原因可能是什么?
class X(object):
def __len__(self):
return 1 << 31
try:
len(X())
except OverflowError:
# 32-bit
MAXSIZE = int((1 << 31) - 1)
else:
# 64-bit
MAXSIZE = int((1 << 63) - 1)
del X
如果我没记错的话,同样的也可以缩短到下面,对吧?
try:
1 << 31
except OverflowError:
# 32-bit
MAXSIZE = int((1 << 31) - 1)
else:
# 64-bit
MAXSIZE = int((1 << 63) - 1)