Python yet again is being asked why something is the way it is and not as someone insists it should be. It is a tool to be used the way it SAYS it works so the bug is perhaps in the user and their expectations.
It is what it is and would break lots of things if changed without much thought. Every other language I know has similar detractors asking why it is the way it is. A useful example where the use of Booleans as 0/1 is commonly used is to find how many of something satisfy some criteria, such as how many items in a list are numbers greater than 65 representing test scores that "passed". It could also be a numpy type of array or a column of a data.frame and so on. The common method is to create a Boolean data structure (often implicitly) that is then viewed as a bunch of 0's and 1's and you can count them to see how many are True (meaning they qualify) or calculate a percentage simply by taking a mean of the 0/1 values. There are other ways, but some also use sums of Booleans to calculate things like any() or all() and I am sure many other things. Yes, all these can be done another way if Booleans were not allowed to be viewed as very small integers. So only use a Boolean in relatively pure situations where arithmetic as a zero or one is meaningful. In other places, maybe don't. The reality though is that if you have 10 apples and you get another one if you flip a coin and it lands on heads. Then 10 + Boolean would indeed be 11. If you have five losing flips (or tickets or whatever) then 5 * Boolean indeed is a zero. 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. 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. -----Original Message----- From: Python-list <python-list-bounces+avi.e.gross=gmail....@python.org> On Behalf Of 2qdxy4rzwzuui...@potatochowder.com Sent: Tuesday, January 24, 2023 6:31 PM To: python-list@python.org Subject: Re: bool and int On 2023-01-25 at 08:58:06 +1100, Chris Angelico <ros...@gmail.com> wrote: > On Wed, 25 Jan 2023 at 08:22, MRAB <pyt...@mrabarnett.plus.com> wrote: > > For backwards compatibility, bool was made a subclass of int. > > Plus, it's really REALLY handy in quite a lot of situations. > > > > C# is pickier, which I guess is a good thing. > > > > Nope, not a good thing. Actually a highly frustrating thing on those > occasions when I have to write C# code. The usual complaint is that some people write FORTRAN no matter what language they're actually using. Are you writing Python in C#? ;-) There's a sweet spot somewhere that includes dynamic typing, high powered global type inference and optimization systems, a thriving community, and a metric [boatload] of rock solid libraries. And an alomost fanatical devotion to the Pope. :-/ -- https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list