On Sat, Jun 15, 2013 at 9:30 PM, Jim Mooney <cybervigila...@gmail.com> wrote: > This is puzzling me. If I check the equality of 0, None, empty > string, and empty list with False, only zero satisfies the equality. > But if I use them in a not statement, they all turn out False. > What gives? > > #Using C:\Python33\python.exe on Win 7 in c:\python33\jimprogs .... > ##Zero is equal to False > ##None is NOT equal to false > ##Empty string is NOT equal to False > ##Empty list is NOT equal to false > ## > ##Zero is equal to False > ##None is equal to false > ##Empty string is equal to False > ##Empty list is equal to False
bool subclasses int, with False == 0 and True == 1. None and empty sequences/mappings aren't *equal* to False. But they are 'falsey' -- i.e. bool(None) is False. The implementation of UNARY_NOT in CPython is based on the function PyObject_IsTrue. This function is hard coded for the singletons True, False, and None -- and otherwise uses either __bool__ (tp_as_number->nb_bool) or __len__ (tp_as_mapping->mp_length or tp_as_sequence->sq_length). A length of 0 is falsey. If neither __bool__ nor __len__ is defined, the object defaults to being truthy: >>> not not object() True >>> not object() False _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor