Names changed to protect the guilty

Antoon Pardon apardon at forel.vub.ac.be
Tue Oct 10 03:34:01 EDT 2006


On 2006-10-10, Ben Finney <bignose+hates-spam at benfinney.id.au> wrote:
> "Andy Salnikov" <a_salnikov at yahoo.com> writes:
>
>> "Aahz" <aahz at pythoncraft.com> wrote:
>> > Antoon Pardon <apardon at forel.vub.ac.be> wrote:
>> >>The problem is there is also ground for bugs if you don't use
>> >>"blah is True". If some application naturally seems to ask for a
>> >>variable that can be valued False, True or a positive integer then
>> >>things like "if var" or "if not var" may very well be a bug too.
>> >
>> > Anyone designing an app like that in Python deserves to lose.
>> > It's just another way of shooting yourself in the foot.
>>
>> OK, I guess nobody ever heard about three-valued logic before,
>> right?
>
> Three-valued logic is fine for some purposes. Bending boolean
> two-valued constants to play the part of three-valued is confusing and
> wrong.

Why? The variable in question gives an answer to the question:

  Has the user requested a connection.

To which the answer can be:

  1: No
  2: Yes
  3: Yes and the connection ID is ...

So tell me what is wrong with using False and True for the simple
No and Yes answer in this case?

>> Of course it does not apply to the original post because has_key()
>> can only return True or False (I hope it will not ever return
>> DontKnow:) but in general if you implement something like 3-valued
>> logic choices like (False,True,None) are almost obvious.
>
> No. Using False and True is a strong signal to the reader that you're
> using *two* value logic. If you break that expectation, the reader
> should not be expected to sympathise.

IMO that expectation is unpythonic. Python allows that a variable
can be of different types during its lifetime. There is even no
mechanism to limit a variable to a specific type. So there is no
reason to expect a variable is limited to False and True just
because one of those was used. Just as there is no reason to
expect a variable will always be an integer just because it
was assigned an integer constant at some point.

> As another poster suggested, if you want to implement three-valued
> logic, use three new objects to represent the states, so the reader
> *knows* there's something going on other than two-value logic. Don't
> re-use False and True in three-valued logic and expect anyone to
> understand your code.

I thought that was the purpose of documentation.

-- 
Antoon Pardon



More information about the Python-list mailing list