bool and int

Chris Angelico rosuav at gmail.com
Thu Jan 26 17:03:26 EST 2023


On Fri, 27 Jan 2023 at 06:32, Dino <dino at no.spam.ar> wrote:
>
> On 1/25/2023 5:42 PM, Chris Angelico wrote:
>
> >
> > Try this (or its equivalent) in as many languages as possible:
> >
> > x = (1 > 2)
> > x == 0
> >
> > You'll find that x (which has effectively been set to False, or its
> > equivalent in any language) will be equal to zero in a very large
> > number of languages. Thus, to an experienced programmer, it would
> > actually be quite the opposite: having it NOT be a number would be the
> > surprising thing!
>
> I thought I had already responded to this, but I can't see it. Weird.
>
> Anyway, straight out of the Chrome DevTools console:
> 
> x = (1>2)
> false
>
> x == 0
> true
>
> typeof(x)
> 'boolean'
>
> typeof(0)
> 'number'
>
> typeof(x) == 'number'
> false
>
> So, you are technically correct, but you can see that JavaScript - which
> comes with many gotchas - does not offer this particular one.
>

That's not what I said, though! What you did in JS was the equivalent of this:

>>> type(True) == type(1)
False

which isn't the same as an isinstance check. Instead, try *exactly
what I said*. You'll find that false is equal to zero and true is
equal to one, just like it is in Python.

ChrisA


More information about the Python-list mailing list