他们都可以把 x 变成一个布尔类型的值:
>>> x = 123
>>> not not x
True
>>> bool(x)
True
>>>
那么谁更快呢?我们写段代码,跑个 100 万次,来比较下谁更快:
import timeit
def bool_convert(x):
return bool(x)
def notnot_convert(x):
return not not x
def main():
trials = 10_000_000
kwargs = {
"setup": "x=42",
"globals": globals(),
"number": trials,
}
notnot_time = timeit.timeit("notnot_convert(x)", **kwargs)
bool_time = timeit.timeit("bool_convert(x)", **kwargs)
print(f"{bool_time = :.04f}")
print(f"{notnot_time = :.04f}")
if __name__ == "__main__":
main()
运行结果如下:
bool(x) 多了 LOAD_GLOBAL 和 CALL_FUNCTION。
这里附一下相关字节码的官方说明:
LOAD_GLOBAL(namei)
Loads the global named co_names[namei] onto the stack.
CALL_FUNCTION(argc)
Calls a callable object with positional arguments. argc indicates the number of positional arguments. The top of the stack contains positional arguments, with the right-most argument on top. Below the arguments is a callable object to call. CALL_FUNCTION pops all arguments and the callable object off the stack, calls the callable object with those arguments, and pushes the return value returned by the callable object.
UNARY_NOT
Implements TOS = not TOS.
不过在日常开发中,我仍然倾向使用 bool(x),因为它的可读性更高,而且,一般也不太可能调用它 100万次。
所以,你会怎么选择呢?