Entry
Boolean type
Jul 5th, 2000 09:59
Nathan Wallace, Hans Nowak, Snippet 84, Barry A. Warsaw
"""
Packages: new_datatypes.boolean
"""
"""
MLH> (Maybe we could have builtin constants "true" and "false"...?
They'd probably need to be objects so that common comparisons will
work as expected. Very quick, very dirty, and I'm sure incomplete
example:
-------------------- snip snip --------------------boolean.py
"""
class Bool:
def __init__(self, flag=0):
self.__flag = flag
def __str__(self):
return self.__flag and 'true' or 'false'
def __cmp__(self, other):
if isinstance(other, Bool):
if other.__flag and self.__flag:
return 0
if not other.__flag and not self.__flag:
return 0
return 1
if other and self.__flag:
return 0
if not other and not self.__flag:
return 0
return 1
true = Bool(1)
false = Bool()
"""
-------------------- snip snip --------------------
Python 1.5.1 (#21, Apr 23 1998, 18:08:12) [GCC 2.8.1] on sunos5
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> from boolean import *
>>> true
<__main__.Bool instance at e1e40>
>>> false
<__main__.Bool instance at c47d8>
>>> print true
true
>>> print false
false
>>> true == 0
0
>>> true == 1
1
>>> 1 == true
1
>>> 1 is true
0
>>> 1 is false
0
>>> [] == true
0
>>> [] == false
1
>>> a = 'hello'
>>> a == false
0
>>> a == true
1
>>> true == false
0
>>> true == true
1
>>> false == false
1
>>>
"""