bool and int

Dino dino at no.spam.ar
Thu Jan 26 12:12:30 EST 2023


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.




More information about the Python-list mailing list