[issue35712] Make NotImplemented unusable in boolean context

Serhiy Storchaka report at bugs.python.org
Wed Mar 11 15:50:30 EDT 2020


Serhiy Storchaka <storchaka+cpython at gmail.com> added the comment:

No.

First, it is impossible. nb_bool and PyObject_IsTrue() can return only three value: 1 for true, 0 for false, and -1 for error. It is not possible to represent NotImplemented without breaking all extensions.

Currently

    if not a:
        b()
    else:
        c()

is equivalent to

    if a:
        c()
    else:
        b()

If a is NotImplemented, what branch be executed in every case?

Second, it would not help. Because real-world examples are not always so trivial as "return not self.__lt__(other)". It may be a part of more complex expression, e.g.:

    return super().__eq__(other) and self.attr == other.attr

So it may help in some examples, but make bugs in other examples even more mystical.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue35712>
_______________________________________


More information about the Python-bugs-list mailing list