[Python-Dev] Boolean type for Py3K?
Barry A. Warsaw
bwarsaw@cnri.reston.va.us
Fri, 17 Mar 2000 15:49:24 -0500 (EST)
>>>>> "KY" == Ka-Ping Yee <ping@lfw.org> writes:
KY> I wondered to myself today while reading through the Python
KY> tutorial whether it would be a good idea to have a separate
KY> boolean type in Py3K. Would this help catch common mistakes?
Almost a year ago, I mused about a boolean type in c.l.py, and came up
with this prototype in Python.
-------------------- snip snip --------------------
class Boolean:
def __init__(self, flag=0):
self.__flag = not not flag
def __str__(self):
return self.__flag and 'true' or 'false'
def __repr__(self):
return self.__str__()
def __nonzero__(self):
return self.__flag == 1
def __cmp__(self, other):
if (self.__flag and other) or (not self.__flag and not other):
return 0
else:
return 1
def __rcmp__(self, other):
return -self.__cmp__(other)
true = Boolean(1)
false = Boolean()
-------------------- snip snip --------------------
I think it makes sense to augment Python's current truth rules with a
built-in boolean type and True and False values. But unless it's tied
in more deeply (e.g. comparisons return one of these instead of
integers -- and what are the implications of that?) then it's pretty
much just syntactic sugar <0.75 lick>.
-Barry