[Tutor] What is the difference between checking false?
Jim Mooney
cybervigilante at gmail.com
Sun Jun 16 05:15:26 CEST 2013
On 15 June 2013 19:45, eryksun <eryksun at gmail.com> wrote:
> On Sat, Jun 15, 2013 at 10:23 PM, eryksun <eryksun at gmail.com> wrote:
>> 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.
I decided to boil it down to what I could remember easily, so this is
the result:
## Comparing different types for equality always fails:
if '5' != 5:
print('oops')
# oops
if '' == False:
print("This will never print.")
## But:
if bool('') == False:
print("Now they're the same type and this will print")
## Now they're the same type and this will print
## And the Python documentation says 'not' doesn't give a damn about types
##
## "Because not has to invent a value anyway, it does not bother to return a
## value of the same type as its argument, so e.g., not 'foo' yields
False, not ''."
##
## Finally, 1 and 0 are oh-so-special standins for True and False,
that should have
## been strangled in the cradle.
--
Jim
After indictment the bacon smuggler was put on the no-fry list
More information about the Tutor
mailing list