Booleans (was: Conditional operator in Python?)

Bruce Sass bsass at freenet.edmonton.ab.ca
Tue Apr 3 16:22:56 EDT 2001


On Tue, 3 Apr 2001, Russell E. Owen wrote:
> In article <mailman.986240224.15817.python-list at python.org>,
>  Bruce Sass <bsass at freenet.edmonton.ab.ca> wrote:
>
> >> But even if the Python development team suddenly became convinced that a
> >> separate boolean type was the way to go, it'd be quite a challenge to
> >> implement such a thing without breaking a vast amount of existing code.
> >
> >Why would adding something to the language break old code...
> >just don't change the old behaviour.
<...>

ok

> Regarding your boolean class, could you explain the value to adding
> "don't know" to it? I can imagine it has its uses, but it sounds like a
> different problem domain than standard conditional logic such as
> if/then/else. If you're aiming for fuzz logic then presumably you'd want
> more choices than simply true, false and don't know.

It was really just a `learning Python' exercise, no specific problem,
but I did have this in mind...

What do you do when you want boolean data from a remote site, but the
link is down.  You could give up and wait for the link to come back,
use a hardcoded default, or defer to something else which returns a
value you can use.  All I did was include the definition of what to do
in the boolean class so the three cases could be handled as such:

# this will raise an exception when the tv of "a" is looked at if
#  get_state returned None
a = Boolean(val=get_state(), assume=None)

# get_state still returns None, but the tv of "a" will be "false"
a = Boolean(val=get_state(), assume=0)

# the tv of a will be whatever what_to_assume returns
a = Boolean(val=get_state(), assume=what_to_assume())

The complete tt looks like this (tv() is a method of Boolean
which lets you turn off the potential exception raising if the tv is
indeterminate):
assume  value  | tv(0)*   tv(1)
------  ------ | -------  -----
 None    None  | TVError  None
 None     0    |  0        0
 None     1    |  1        1
  0      None  |  0        0
  0       0    |  0        0
  0       1    |  1        1
  1      None  |  1        1
  1       0    |  0        0
  1       1    |  1        1

* operations (e.g., &, |, not, etc.) act like tv(0)

How useful these semantics are in the real world is something I could
only guess at, but one could sure come up with some hard to figure but
succinct code if the obj and assume parameters are themselves
tri-state "booleans" (and you happen to need that kind of complicated
logical functionality in enough places to warrant adding yet another
class to your code).


The only fuzzy logic I've seen is the implementation in logic.py
at the Vaults of Parnassus, it looks more like a probability thing
than logic <shrug>.


- Bruce





More information about the Python-list mailing list