bool()和operator.truth()之间有什么区别? [英] What are the differences between bool() and operator.truth()?

查看:63
本文介绍了bool()和operator.truth()之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

bool()

bool() and operator.truth() both test whether a value is truthy or falsy and they seem rather similar from the docs, it even says in the truth() docs that:

这等效于使用bool构造函数.

This is equivalent to using the bool constructor.

但是,通过简单测试,truth()的速度是bool()的两倍(显示了Python 3.6的计时,但2.7相似):

However, truth() is over twice as fast as bool() from a simple test (Python 3.6 timings shown, but 2.7 is similar):

from timeit import timeit
print(timeit('bool(1)', number=10000000))
# 2.180289956042543
print(timeit('truth(1)', setup='from operator import truth', number=10000000))
# 0.7202018899843097

那有什么区别?我应该使用truth()代替bool()吗?

So what are the differences? Should I use truth() instead of bool()?

在与 ShadowRanger 进行广泛评论和讨论之后,出现了此问题与解答.com/questions/48909056/creating-a-list-within-a-list-in-python/>这个问题.

This Q&A arose after extensive comments and discussion with ShadowRanger under this question.

推荐答案

尽管bool()operator.truth() output 在主要用例的实现中具有相同的结果实际上是完全不同的. bool()是类或类型的构造函数,而truth()是狭窄的优化常规函数.

Although bool() and operator.truth() output the same result for the major uses cases their implementation is actually rather different. bool() is a class or type constructor while truth() is a narrow optimised regular function.

实际上,还有两个区别:1)bool()在不带参数的情况下调用return s False,而truth()需要一个参数. 2)bool()接受x关键字参数,例如bool(x=1),而truth()不接受关键字参数.对于常规用例,这两个都会增加bool()的开销.

In practical terms, there are also two differences: 1) bool() called with no arguments returns False while truth() requires an argument. 2) bool() accepts an x key word argument, like bool(x=1), while truth() takes no keyword arguments. Both of these add overhead to bool() for the regular use cases.

关键字实现是奇怪的,因为可能没有人需要它,并且名称x几乎没有描述性. 问题29695 涵盖了这一点,实际上,该问题不仅影响bool(),而且影响其他类,例如int()list().但是,从Python 3.7开始,这些关键字参数将被删除,并且速度应会提高.尽管如此,我还是在最新的Python 3.8分支上测试了时序,并且bool()比以前更快,但仍然是truth()的两倍,这可能是由于bool()的实现更为通用.

The key word implementation is odd since likely no-one needs it and the name x is hardly descriptive. Issue29695 covers this, and in fact the issue impacts not just bool() but other classes like int() or list(). However, from Python 3.7 onwards these key word arguments will be removed, and speed should improve. Nonetheless, I tested the timings on the latest Python 3.8 branch, and bool() is faster than before but still over twice as slow as truth(), presumably due to the more generic implementation of bool().

因此,如果您的任务非常重视速度,那么我建议您在需要功能的情况下使用truth()而不是bool()(例如,将其解析为sorted()的键).但是,正如 khelwood 指出的那样,bool()有时仍可能会更快,例如filter(bool, iterable),因此可能最好确定您的用例的时间,以便确定最佳选择.

So, if you have a task where speed is of high importance I would recommend using truth() over bool() if you require a function (for example to parse as a key to sorted()). However, as khelwood points out, bool() can still be faster occasionally, such as filter(bool, iterable), so it is probably best to time your use case to be certain of the best option.

当然,如果您不需要功能,而只想测试值是 truthy 还是 falsy ,则应使用惯用的ifif not语句,最快的是khelwood和 user2357112 进行评论.

Of course, if you don't need a function and simply want to test if a value is truthy or falsy you should use the idiomatic if or if not statements, which are fastest as khelwood and user2357112 commented.

在与 ShadowRanger 进行广泛评论和讨论之后,出现了此问题与解答.com/questions/48909056/creating-a-list-within-a-list-in-python/>这个问题.

This Q&A arose after extensive comments and discussion with ShadowRanger under this question.

这篇关于bool()和operator.truth()之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆