Newbie: Truth values (three-valued logic)
Olaf Delgado
delgado at Mathematik.Uni-Bielefeld.DE
Tue Jun 15 15:07:25 EDT 1999
Hi everybody!
I am just starting to use python seriously. Currently, I am porting a
small class library from C++ to python, which is kind of fun. Everything's
much more simple and elegant now.
There's one thing I could do in C++, though, which doesn't seem to work in
python: some methods need to return truth values, which can be 1 for
'true', 0 for 'false' and x for 'frankly I don't know'. Now this x,
when used in an if-clause, should evaluate to false, which is easy:
class uncertain:
def __nonzero__(self):
return 0
def __repr__(self):
return 'dunno'
So the following works fine:
>>> maybe = uncertain()
>>> maybe
dunno
>>> if maybe: print 't' # prints nothing
...
>>>
Unfortunately:
>>> if not maybe: print 'f'
...
f
>>>
This behaviour is completely consistent in a two-valued logic, but not
what I actually want, because maybe should neither be true nor false, nor
should its negation. As far as I know, the logical negation operator can
not be overloaded, so I have no chance to change this, right? No big deal,
really, but I'd like to know if I missed something here. So, is there some
magical trickery to achieve the behaviour I want?
certainly-uncertain'ly yours, Olaf
--
//// Olaf Delgado Friedrichs, Uni Bielefeld, Mathematik
Olaf Postfach 100131 (room: V4-109)
`=' D-33501 Bielefeld, Germany (phone: +49 521 106-4765)
http://www.mathematik.uni-bielefeld.de/~delgado
More information about the Python-list
mailing list