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
-
Is there something else I’m missing which is used for truth-testing?
-
Are there any other objects (besides
None) which evaluate toFalsethat implement neither of__len__or__nonzero__?
bool(x)isTrueifxis an object without one of the magic methods you mentioned returningFalse. That’s whyEllipsisevaluates toTrue.Noneis special-cased inbool()and makes it returnFalse.Details:
bool()usesPyObject_IsTrue()API function which in 2.7.2 looks like this: