RE: True inconsistency in Python

Discussion in 'Python' started by Jimmy Retzlaff, Nov 17, 2003.

  1. 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
     
    Jimmy Retzlaff, Nov 17, 2003
    #1
    1. Advertising

  2. Jimmy Retzlaff

    KefX Guest

    >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
     
    KefX, Nov 18, 2003
    #2
    1. Advertising

  3. Jimmy Retzlaff

    Terry Reedy Guest

    "Jimmy Retzlaff" <> wrote in message
    news:...

    >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
     
    Terry Reedy, Nov 18, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Siemel Naran

    Does true ^ true return false?

    Siemel Naran, Jun 17, 2004, in forum: C++
    Replies:
    19
    Views:
    669
    Chris Theis
    Jun 18, 2004
  2. Chip
    Replies:
    6
    Views:
    2,637
    E. Robert Tisdale
    Jan 8, 2005
  3. Scott Chapman

    True inconsistency in Python

    Scott Chapman, Nov 13, 2003, in forum: Python
    Replies:
    46
    Views:
    968
    Ron Adam
    Nov 19, 2003
  4. Andy Leszczynski
    Replies:
    4
    Views:
    332
    Erik Max Francis
    Oct 13, 2005
  5. bdb112
    Replies:
    45
    Views:
    1,348
    jazbees
    Apr 29, 2009
Loading...

Share This Page