math.isinf()测试正或负无限集中在一起.什么是pythonic方式明确地测试它们?
测试正无穷大的方法:
x == float('+inf')math.isinf(x) and x > 0测试负无穷大的方法:
x == float('-inf')math.isinf(x) and x < 0拆卸方式1:
>>> def ispinf1(x): return x == float("inf")
...
>>> dis.dis(ispinf1)
1 0 LOAD_FAST 0 (x)
3 LOAD_GLOBAL 0 (float)
6 LOAD_CONST 1 ('inf')
9 CALL_FUNCTION 1
12 COMPARE_OP 2 (==)
15 RETURN_VALUE
Run Code Online (Sandbox Code Playgroud)
拆卸方式2:
>>> def ispinf2(x): return isinf(x) and x > 0
...
>>> dis.dis(ispinfs)
1 0 LOAD_GLOBAL 0 (isinf)
3 LOAD_FAST 0 (x)
6 CALL_FUNCTION 1
9 JUMP_IF_FALSE_OR_POP …Run Code Online (Sandbox Code Playgroud)