bool and int

Chris Angelico rosuav at gmail.com
Tue Jan 24 21:01:24 EST 2023


On Wed, 25 Jan 2023 at 12:43, <avi.e.gross at gmail.com> wrote:
> Python has a different philosophy than some other languages with strong
> typing. In some of those, you would not be allowed to add or multiply at
> random but would need to convert parts of your calculation to all be the
> same, such as a 32-bit integer. You could still do things like I mention
> above but only after consciously mapping your Boolean to an actual zero or
> one of the kind wanted.

Python is strongly dynamically typed. You may be thinking of "static
typing" rather than "strong typing" here, and there are plenty of
strongly statically typed languages that allow you to do arithmetic on
booleans (with them usually behaving as if False is 0 and True is 1,
although not always).

> I worry a tad more about the other direction where something like an integer
> containing a number like 12 is used in a context where it gets downgraded to
> a True/False and later may inadvertently be used as a "1" as the conversion
> is not anticipated. There is data loss there more than in the case of a
> Boolean becoming a 1.

Well, yes, but that's no different from a float like 6.25 getting
downgraded to an integer 6. There's a reason that most languages
upcast silently but don't downcast without being asked to.

ChrisA


More information about the Python-list mailing list