True inconsistency in Python
radam2 at tampabay.rr.com
Mon Nov 17 18:40:20 CET 2003
On Mon, 17 Nov 2003 00:19:11 -0800, Erik Max Francis <max at alcyone.com>
>Tim Roberts wrote:
>> Python is certainly not the only language in which this occurs. Many
>> the C programmers who have been burned by:
>> int SomeFunction();
>> if( SomeFunction == TRUE )
>I think you're getting burned by something else, there :-).
>> Visual Basic has exactly the same problem.
>It's not a problem, explicitly comparison with a Boolean literal,
>whether or not Booleans are not distinct types (Lisp, C89), are distinct
>types with implicit conversion (C++, Python), or are distinct types in
>which implicit conversions are disallowed (Java) is completely
>superfluous. The proper way to test whether an expression is true is
> if (expression)
To me the usefulness of using True and False is that it is defined to
values consistent with the programming language that you are using.
So using them to assign x = True, or x = False. insures that when I
x = True
So I get consistent results for the language and platform I'm using
now and in the future. If down the road someone decided to make True
= 3, and False = -5, and they change the language so that any boolean
comparisons return 3 and -5 respectively, my use of True and False
will still work. If I tested for 1 or 0 using the 'if x:' method,
then my program will break.
x = 1
if x: # returned value undefined, x is not 3 or -5
So I do an explicit test 'if (x=1)' to make sure it will work even if
'True' gets redefined.
So we should either use explicit comparisons of values, or use only
True and False, but not mix them.
>What is the point of the explicit test with the true constant? What
>about the result of _that_ comparison, shouldn't that be tested with
>true as well -- ((expression == TRUE) == TRUE)? But what about _that_
>result, shouldn't it be tested too?
>Explicit comparison with the true constant (or false constant)
>necessarily degenerates into complete uselessness.
>> Further, until very
>> True in VB actually evaulated to -1, so even comparing to "1" would
>That's because in traditional BASIC, "true" is all-bits on. The true
>constant wasn't 1 in the first place.
>> >Assuming the old behavior is desired, programmers need to be careful
>> >not to compare a variable with True as in:
>> >if var == True: # only works if var is 1
>> > blah
>> Your statement is absolutely true. End of story.
>It's true in that that might cause a problem, but it's not true that
>that's undesirable language behavior. It's programmer error -- in any
>language that has a Boolean true (or false) literal.
It looks to me that 'True' in python is a combination of the boolean
binary logic of 1 or 0, and as an "exists" test of 0 or not 0.
If python had an exists operator, you could do.
if x exists:
This could serve two options also... does the object exist? or does
a value exist where the value is not equal to zero or None. There
would need to be a syntax difference between the two.
Using the 'if x:' form does this for values and is clear enough. So
using an 'exists()' function to check for the presence of an object
would be useful if there isn't a simple way to do it already. Right
now it you do this without first assigning a value to x you get:
>>> if x:
Traceback (most recent call last):
File "<pyshell#2>", line 1, in -toplevel-
NameError: name 'x' is not defined
There is an os.path.exists() function for checking if a file path
exists. A more general exists() function or method my be useful and
still be consistent with it.
With pythons dynamic variables, I think an exists function would be
useful to check if an object exists. And to use 'if x:' to check if
a (value!=0) exists. It's clear what is being tested for from the
I can't do:
if not exists(object):
object = 0
This generates a not defined error if object isn't defined when exists
So I have to do:
exist = True
exist = False
if not exist:
object = 0
That can be shortened to:
object = 0
This isn't to bad, but I would prefer something like,
if not object.exists: # does the abject already exist?
object = 0 # if not, create and set initial value
# else use the existing value
I think I diverged from True or False a bit here. And I'm not sure if
this functionality already exists or not?
There's probably a way to do this in python already.
More information about the Python-list