bool and int

Chris Angelico rosuav at gmail.com
Wed Jan 25 17:42:48 EST 2023


On Thu, 26 Jan 2023 at 08:19, Dino <dino at no.spam.ar> wrote:
>
> On 1/23/2023 11:22 PM, Dino wrote:
> >  >>> b = True
> >  >>> isinstance(b,bool)
> > True
> >  >>> isinstance(b,int)
> > True
> >  >>>
>
> ok, I read everything you guys wrote. Everyone's got their reasons
> obviously, but allow me to observe that there's also something called
> "principle of least surprise".
>
> In my case, it took me some time to figure out where a nasty bug was
> hidden. Letting a bool be a int is quite a gotcha, no matter how hard
> the benevolent dictator tries to convince me otherwise!
>

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!

ChrisA


More information about the Python-list mailing list