Implement a Tribool data type. This is a data type that can have one of three
values, True, False, and unknown (for which use None). In addition to
__init__(), implement __str__(), __repr__(), __cmp__(),
__nonzero__() for conversion to bool(), __invert__() for logical not
(~), __and__() for logical and (&), and __or__() for logical or (|). There
are two possible logics that can be used: propagating, where any expression
involving unknown (i.e., None) is unknown, and non-propagating where any
expression involving unknown that can be evaluated is evaluated. Use nonpropagating logic so that your Tribools match the truth table, and where t
is Tribool(True), f is Tribool(False) and n is Tribool(None) (for
unknown):
Expression Result Expression Result Expression Result
~t False ~f True ~n None
t & t True t & f False t & n None
f & f False f & n False n & n None
t | t True t | f True t | n True
f | f False f | n None n | n None
For example, with non-propagating logic, True | None, is True, because so
long as one operand to logical or is true, the expression is true. But False |
None is None (unknown), because we cannot determine the result.
Most of the methods can be implemented in just a few lines of code. Make sure
you use the doctest module and write unit tests for all the methods