[Python-Dev] Boolean transition (original) (raw)

Paul Prescod paul@prescod.net
Sun, 10 Mar 2002 07:37:33 -0800


Tim Peters wrote:

Java disallowed mixing ints with booleans specifically to stop the if (x = 1) flavor of C bug, but Python stops those via a different gimmick.

I don't know if that's true or not, but it doesn't seem relevant. Python's decision should be independent of it's C heritage so we need to look not only at why C-derived languages did what they did but also other languages. Most languages with no C heritage at all have a non-numeric boolean type. Here are other languages that (based on some admittedly cursory research) treat booleans as entirely distinct from integers:

I'd be curious to hear a list of high level, GC'd languages in the other camp. Methinks your Fortran/C bias shows. ;) And it isn't pretty.

... There's nothing reprehensible about, e.g.,

daysinyear = 365 + isleap(year) or print [("off", "on")[switch] for switch in switchlist]

Personally, I find those both as obfuscatory workarounds for the lack of a terniary. I don't mind spelling out a terniary as an if/else and I don't mind spelling these out either. Plus the latter can be done more easily and clearly with a dictionary:

print [{true:"on", false:"off"}[switch] for switch in switch_list]

Ban sensible uses, and people will just keep using integers for their true/false, on/off, set/clear etc values.

If the comparison operators etc. return booleans then people will use booleans. I'd be amazed if many people avoided them because they wanted to do tricks like those above. AFAIK, they don't in any of the other programming languages.

Paul Prescod