[Tutor] What is the difference between checking false?
Joel Goldstick
joel.goldstick at gmail.com
Sun Jun 16 05:48:06 CEST 2013
On Sat, Jun 15, 2013 at 11:15 PM, Jim Mooney <cybervigilante at gmail.com>wrote:
> 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.
>
One and zero for True and False may seem not quite right today, but digital
computers are based on the fact that circuits can be built that have two
states -- on/off or true/false, or 1/0. Computers at their hardware core
are complicated state machines where the all the states are either True or
False. So really, it made sense to make the mapping between a gate that is
in one of two states to be represented by True/False.
>
> --
> Jim
> After indictment the bacon smuggler was put on the no-fry list
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
--
Joel Goldstick
http://joelgoldstick.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130615/9f945d73/attachment-0001.html>
More information about the Tutor
mailing list