Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   RE: True inconsistency in Python (http://www.velocityreviews.com/forums/t324986-re-true-inconsistency-in-python.html)

Jimmy Retzlaff 11-17-2003 08:01 PM

RE: True inconsistency in Python
 
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



KefX 11-18-2003 01:16 AM

RE: True inconsistency in Python
 
>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


Terry Reedy 11-18-2003 05:41 AM

Re: True inconsistency in Python
 

"Jimmy Retzlaff" <jimmy@retzlaff.com> wrote in message
news:mailman.811.1069099332.702.python-list@python.org...

>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




All times are GMT. The time now is 08:39 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.