Python bool(省略号) 和 bool(无)

发布于 2024-12-23 20:04:36 字数 581 浏览 1 评论 0原文

我不明白 EllipsisNone 如何由 bool() 进行不同的处理,因为两者在相关属性方面似乎相同用于真理检验。

>>> bool(Ellipsis)
True
>>> bool(None)
False
>>> any([hasattr(Ellipsis, attr) for attr in ['__len__', '__bool__', '__nonzero__']])
False
>>> any([hasattr(None, attr) for attr in ['__len__', '__bool__', '__nonzero__']])
False
  1. 我还缺少其他用于真理测试的东西吗?

  2. 是否有任何其他对象(除了 None 之外)计算结果为 False 且既不实现 __len__ 也不实现 __nonzero__ >?

I don't understand how are Ellipsis and None handled differently by bool(), when both seem to be identical in terms of the relevant attributes for truth-testing.

>>> bool(Ellipsis)
True
>>> bool(None)
False
>>> any([hasattr(Ellipsis, attr) for attr in ['__len__', '__bool__', '__nonzero__']])
False
>>> any([hasattr(None, attr) for attr in ['__len__', '__bool__', '__nonzero__']])
False
  1. Is there something else I'm missing which is used for truth-testing?

  2. Are there any other objects (besides None) which evaluate to False that implement neither of __len__ or __nonzero__?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦在深巷 2024-12-30 20:04:36

如果 x 是一个对象,并且没有您提到的返回 False 的魔术方法之一,则 bool(x)True。这就是 Ellipsis 计算结果为 True 的原因。

None特殊情况 bool() 并使其返回 False

详细信息:

bool() 使用 PyObject_IsTrue() API 函数,在 2.7.2 中如下所示:

int
PyObject_IsTrue(PyObject *v)
{
    Py_ssize_t res;
    if (v == Py_True)
        return 1;
    if (v == Py_False)
        return 0;
    if (v == Py_None)
        return 0;
    else if (v->ob_type->tp_as_number != NULL &&
             v->ob_type->tp_as_number->nb_nonzero != NULL)
        res = (*v->ob_type->tp_as_number->nb_nonzero)(v);
    else if (v->ob_type->tp_as_mapping != NULL &&
             v->ob_type->tp_as_mapping->mp_length != NULL)
        res = (*v->ob_type->tp_as_mapping->mp_length)(v);
    else if (v->ob_type->tp_as_sequence != NULL &&
             v->ob_type->tp_as_sequence->sq_length != NULL)
        res = (*v->ob_type->tp_as_sequence->sq_length)(v);
    else
        return 1;
    /* if it is negative, it should be either -1 or -2 */
    return (res > 0) ? 1 : Py_SAFE_DOWNCAST(res, Py_ssize_t, int);
}

bool(x) is True if x is an object without one of the magic methods you mentioned returning False. That's why Ellipsis evaluates to True.

None is special-cased in bool() and makes it return False.

Details:

bool() uses PyObject_IsTrue() API function which in 2.7.2 looks like this:

int
PyObject_IsTrue(PyObject *v)
{
    Py_ssize_t res;
    if (v == Py_True)
        return 1;
    if (v == Py_False)
        return 0;
    if (v == Py_None)
        return 0;
    else if (v->ob_type->tp_as_number != NULL &&
             v->ob_type->tp_as_number->nb_nonzero != NULL)
        res = (*v->ob_type->tp_as_number->nb_nonzero)(v);
    else if (v->ob_type->tp_as_mapping != NULL &&
             v->ob_type->tp_as_mapping->mp_length != NULL)
        res = (*v->ob_type->tp_as_mapping->mp_length)(v);
    else if (v->ob_type->tp_as_sequence != NULL &&
             v->ob_type->tp_as_sequence->sq_length != NULL)
        res = (*v->ob_type->tp_as_sequence->sq_length)(v);
    else
        return 1;
    /* if it is negative, it should be either -1 or -2 */
    return (res > 0) ? 1 : Py_SAFE_DOWNCAST(res, Py_ssize_t, int);
}
温暖的光 2024-12-30 20:04:36

来自 Python 数据模型

如果一个类既没有定义 len() 也没有定义 nonzero(),则它的所有
实例被认为是真实的。

None 计算结果为 false,因为它是内置类型 指定这样做。正如您所说,您没有在 Ellipsis 上定义 __len__()__nonzero__() 。如果你想让它评估为 false,

class Ellipsis(...):
  #...
  def __nonzero__(self):
      return False

  # or
  def __len__(self):
      return 0

From the Python data model:

If a class defines neither len() nor nonzero(), all its
instances are considered true.

None evaluates to false because it's a built-in type that is specified to do so. You did not define __len__() or __nonzero__() on Ellipsis, as you stated. If you want it to evaluate to false,

class Ellipsis(...):
  #...
  def __nonzero__(self):
      return False

  # or
  def __len__(self):
      return 0
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文