Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > RE: True inconsistency in Python

Reply
Thread Tools

RE: True inconsistency in Python

 
 
Jimmy Retzlaff
Guest
Posts: n/a
 
      11-17-2003
KefX wrote:
....
> I'm not so sure about that. In most languages (including Python),

'false'
> is guaranteed to be only one value (usually zero), therefore comparing
> against it poses no problem. I assume you're saying that doing so

isn't
> problematic in any way, it's just pointless.


In Python the singleton False has exactly one value (as does the
singleton True), but there are many values that are treated as false
when used as an expression being evaluated by "if" including 0, None,
and empty containers ([], {}, "", sets.Set([]), etc.). Comparing a value
directly to False in Python is not a good idea. So, for example, the
following two "if" statements are not equivalent for all possible values
of x:

if not x:
...

if x == False:
...

The first will execute the body if x == [], the second will not.

....
> But if whatever goes in "blah" is really long (and often it is), you

very
> quickly see what is being compared: it's a straight boolean

comparison,
> whereas with the second you have to look at the whole thing, find no
> comparison operator, and go, "oh, there's no explicit comparison so

it's
> obviously an implicit straight boolean comparison". This is especially
> valuable in really long 'if' statements

....

I agree with Michael Geary that skipping the comparison to True/False
reads much more cleanly. If the expression is too long to be clear, then
you can (should?) encapsulate it in a function or variable that gives
the expression a short descriptive name:

if mouseClickPosition.InRectangle(self.left, self.top,
self.right, self.bottom) == False:
...


would probably be more clear like this:


mouseClickInBoundingBox = mouseClickPosition.InRectangle(
self.left, self.top,
self.right, self.bottom)
if mouseClickInBoundingBox:
...

....
> Although, as a last thought, it should be relatively easy to make it

so
> that True returns '1' in any comparison except against 0. A class

would be
> able to do this easily by overriding __cmp__:
>
> def true_class(bool):
> def __cmp__(self, other):
> if other:
> return 1
> else:
> return 0
>
> True = true_class()

....

Similar functionality is already in recent versions of Python:

>>> bool([])

False
>>> bool([1, 2, 3])

True

So, as I believe was pointed out earlier in this thread, if you're
really eager to compare to True and/or False you can do the following
safely:

if bool(x) == True:
...

if bool(x) == False:
...


Jimmy


 
Reply With Quote
 
 
 
 
KefX
Guest
Posts: n/a
 
      11-18-2003
>In Python the singleton False has exactly one value (as does the
>singleton True), but there are many values that are treated as false
>when used as an expression being evaluated by "if" including 0, None,
>and empty containers ([], {}, "", sets.Set([]), etc.).


Oh, right! I completely missed this. Yes, that does make a strong case against
comparing against booleans in Python, unless you cast to bool, which is ugly,
which is another strike against doing it.

That particular point doesn't say anything about languages where 'false' is
most definitely only one value, though.

- Kef

 
Reply With Quote
 
 
 
 
Terry Reedy
Guest
Posts: n/a
 
      11-18-2003

"Jimmy Retzlaff" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

>the singleton False has exactly one value (as does the
>singleton True


Reworded: NoneType is a singleton type whose singleton instance has
value None and is initially bound to the name None. bool is a
doubleton type whose two and only two instances have values True and
False and are initially bound to the names True and False.

> Comparing a value directly to False in Python is not a good idea.


Unless that is exactly what you want to do.

> So, for example, the following two "if" statements are not

equivalent
> for all possible values of x:


> if not x:
> if x == False:


Which is why one might possibly want the second, except that it can be
written for faster execution (because the False object is unique) as

if x is False:

Terry J. Reedy


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[False,True] and [True,True] --> [True, True]????? bdb112 Python 45 04-29-2009 02:35 AM
TurboGears /.-ed, >new == True< or >new == "True"< Andy Leszczynski Python 4 10-13-2005 06:56 AM
C and C++ are interoperable languages? True or Not True? Chip C++ 6 01-08-2005 11:10 PM
Does true ^ true return false? Siemel Naran C++ 19 06-18-2004 11:06 AM
True inconsistency in Python Scott Chapman Python 46 11-19-2003 01:46 AM



Advertisments