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

Greg Ewing greg@cosc.canterbury.ac.nz
Mon, 11 Mar 2002 14:28:54 +1300 (NZDT)


Tim Peters <tim.one@comcast.net>:

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

daysinyear = 365 + isleap(year)

This is bug-prone, because it relies on is_leap always returning a "normalised" boolean value. I don't think it would be a bad thing if you had to write it

days_in_year = 365 + ord(is_leap(year))

or, even better,

days_in_year = 365 + bit(is_leap(year))

where bit() is a function that takes a boolean (and only a boolean) and returns 0 or 1.

print [("off", "on")[switch] for switch in switchlist]

No, no, what you really mean is

print [{False:"off", True:"on"}[switch] for switch in switch_list]

or perhaps

print ["on" if switch else "off" for switch in switch_list]

and similarly

days_in_year = 365 + 1 if is_leap(year) else 0

(Yes, I know that one's already been rejected. I can't stop liking it, though...)

Greg Ewing, Computer Science Dept, +--------------------------------------+ University of Canterbury, | A citizen of NewZealandCorp, a | Christchurch, New Zealand | wholly-owned subsidiary of USA Inc. | greg@cosc.canterbury.ac.nz +--------------------------------------+