Steve Jorgensen wrote: > I realize this is probably something that would be hard to change for > compatibility > reasons. Maybe someone can think of a way around that though? > It seems to me that not NotImplemented should result in > NotImplemented and attempting to convert it to bool should raise > a TypeError exception. > Take the following example: > def __lt__(self, other): > return not self.__ge__(other): > > def __le__(self, other): > return not self.__gt__(other): > > def __ge__(self, other): > <some code that might or might not return NotImplemented> > > Currently, this will not work because NotImplemented is truthy and > not NotImplemented is False, so it is necessary to complicate > the implementations of __lt__ and __le__ to specifically check > whether the value returned from the complementary method returned > NotImplemented or not. > If the value of not NotImplemented was NotImplemented then > the coding pattern above would simply work.
Yeah, it seems contra-intuitive. But there is some logic in that behaviour. `not NotImplemented` is a boolean test and, I think, it follows the "Truth Value Testing" (https://docs.python.org/3/library/stdtypes.html#truth). According to that, `bool(NotImplemented)` returns True so `not bool(NotImplemented)` returns False. Ultimately, the NotImplemented singleton is like any other Python object and, from some point of view, it is logical that it behaves like other objects. From that point of view, `not NotImplemented` must not return `NotImplemented` ever --it is not a boolean value! Raising a TypeError when executing `bool(NotImplemented)`? Why? In my understanding, if you are comparing two object with different types (which cannot be compared), False is not a wrong answer. Remember that NotImplemented is a "special value which should be returned to indicate that the operation is not implemented with respect to the other type" and its purpose is that "the interpreter will try the reflected operation on the other type" (https://docs.python.org/3/library/constants.html#NotImplemented). So, IMHO, you should check if __ge__ returns a NotImplemented singleton or not and react accordingly to the result of that test. _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/BDCOTQVEHG4Q7MYBLNGNXA2W36FAUP7Y/ Code of Conduct: http://python.org/psf/codeofconduct/