True inconsistency in Python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Scott Chapman

    True inconsistency in Python

    There seems to be an inconsistency here:

    Python 2.3.2 (#1, Oct 3 2003, 19:04:58)
    [GCC 3.2 20020903 (Red Hat Linux 8.0 3.2-7)] on linux2[color=blue][color=green][color=darkred]
    >>> 1 == True[/color][/color][/color]
    True[color=blue][color=green][color=darkred]
    >>> 3 == True[/color][/color][/color]
    False[color=blue][color=green][color=darkred]
    >>> if 1: print "true"[/color][/color][/color]
    ....
    true[color=blue][color=green][color=darkred]
    >>> if 3: print "true"[/color][/color][/color]
    ....
    true
    [color=blue][color=green][color=darkred]
    >>> 0 == False[/color][/color][/color]
    True[color=blue][color=green][color=darkred]
    >>> -1 == False[/color][/color][/color]
    False[color=blue][color=green][color=darkred]
    >>> -1 == True[/color][/color][/color]
    False[color=blue][color=green][color=darkred]
    >>> if -1: print "true"[/color][/color][/color]
    ....
    true[color=blue][color=green][color=darkred]
    >>> if -2: print "true"[/color][/color][/color]
    ....
    true
    [color=blue][color=green][color=darkred]
    >>> if None:[/color][/color][/color]
    .... print "hello"
    ....[color=blue][color=green][color=darkred]
    >>> x = None
    >>> x == None[/color][/color][/color]
    True[color=blue][color=green][color=darkred]
    >>> x == True[/color][/color][/color]
    False[color=blue][color=green][color=darkred]
    >>> x == False[/color][/color][/color]
    False[color=blue][color=green][color=darkred]
    >>> x <> True[/color][/color][/color]
    True[color=blue][color=green][color=darkred]
    >>> x <> False[/color][/color][/color]
    True[color=blue][color=green][color=darkred]
    >>> None == True[/color][/color][/color]
    False[color=blue][color=green][color=darkred]
    >>> None == False[/color][/color][/color]
    False


    Historically Python has allowed <> 0 to equal true in evaluations. Now
    <> 0 still evaluates to true in evaluations. However it doesn't equal
    True. They are not interchangable. (Same with empty lists, etc.)
    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

    ' Must use:

    if var: # works if var is not 0
    blah

    Is this inconsistency going to be resolved in the future?

    How? (Will <>0 == True or will <>0 <> true or will it be resolved some
    other way that I'm too opaque to see? :-)

    It seems that maybe Python should throw a warning (perhaps if a flag is
    set) any time it bumps into code comparing a variable to True or False.
    It's pretty subtle and would easily throw a newbie.

    Of course, according to the above logic, if 1 == true and 2 == true,
    then 1 == 2! Thankfully this doesn't work in Python. Python is magic.
    I love the magic but it creates some interesting inconsistencies .

    Scott


  • Erik Max Francis

    #2
    Re: True inconsistency in Python

    Scott Chapman wrote:
    [color=blue]
    > Historically Python has allowed <> 0 to equal true in evaluations.
    > Now
    > <> 0 still evaluates to true in evaluations. However it doesn't equal
    > True. They are not interchangable. (Same with empty lists, etc.)[/color]

    That's because the proper way to test for truth does not use the True
    value at all. It is this:

    if x:
    ...

    not this:

    if x == True:
    ...

    --
    Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
    __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
    / \ Life is a predicament which precedes death.
    \__/ Henry James

    Comment

    • David C. Fox

      #3
      Re: True inconsistency in Python

      Scott Chapman wrote:
      [color=blue]
      >
      > if var == True: # only works if var is 1
      > blah
      >
      > ' Must use:
      >
      > if var: # works if var is not 0
      > blah[/color]

      Just because something isn't True doesn't mean it isn't true.

      David

      Comment

      • Ben Finney

        #4
        Re: True inconsistency in Python

        On Wed, 12 Nov 2003 19:42:27 -0800, Scott Chapman wrote:[color=blue]
        > There seems to be an inconsistency here:[/color]

        Yes. The inconsistency is in expecting all Boolean truths to be the
        same value.

        The object True will evaluate as a Boolean truth. The object False will
        not evaluate as a Boolean truth. This doesn't mean that there are no
        other values that will or won't evaluate as Boolean truth.

        You many want to read the PEP that led to the creation of the 'bool'
        type (and True and False objects):

        <http://www.python.org/peps/pep-0285.html>

        In short: Testing the Boolean truth of an expression is done with 'if',
        not with value-comparison operators.

        --
        \ "bash awk grep perl sed, df du, du-du du-du, vi troff su fsck |
        `\ rm * halt LART LART LART!" -- The Swedish BOFH, |
        _o__) alt.sysadmin.re covery |
        Ben Finney <http://bignose.squidly .org/>

        Comment

        • Ron Adam

          #5
          Re: True inconsistency in Python

          On Wed, 12 Nov 2003 19:42:27 -0800, Scott Chapman
          <scott_list@mis chko.com> wrote:
          [color=blue]
          >
          >Historically Python has allowed <> 0 to equal true in evaluations. Now
          ><> 0 still evaluates to true in evaluations. However it doesn't equal
          >True.[/color]

          [color=blue][color=green][color=darkred]
          >>>a = (2 != 0)
          >>>a == True[/color][/color][/color]
          True

          Seems to work for me. Am I missing something?

          The only thing that surprises me in all of this is the "if var:"
          evaluating to true for numbers other than 1. That's new to me, I
          would have expected an exception in that case. But it still makes
          since if I look at it as a shortcut for "if (var != 0):". This
          only proves I'm still relatively new to Python I think.

          _Ron Adam


          Comment

          • Ben Finney

            #6
            Re: True inconsistency in Python

            On Thu, 13 Nov 2003 06:26:09 GMT, Ron Adam wrote:[color=blue]
            > The only thing that surprises me in all of this is the "if var:"
            > evaluating to true for numbers other than 1. That's new to me, I
            > would have expected an exception in that case.[/color]

            Python has only recently gained a Boolean type ('bool').

            <http://www.python.org/peps/pep-0285.html>

            Before that, Boolean logic was done with integer values. Zero equated
            to Boolean false, non-zero equated to Boolean true; and the default
            Boolean true value was simply the integer 1.

            This conflation of types is confusing, and (like many other languages)
            Python has now "grown a Boolean type" to distinguish integer 0 and 1
            from Boolean False and True. However, the previous behaviour is still
            supported -- for how long, I don't know.

            --
            \ "Injustice is relatively easy to bear; what stings is justice." |
            `\ -- Henry L. Mencken |
            _o__) |
            Ben Finney <http://bignose.squidly .org/>

            Comment

            • Ron Adam

              #7
              Re: True inconsistency in Python

              On 13 Nov 2003 17:08:13 +1050, Ben Finney
              <bignose-hates-spam@and-benfinney-does-too.id.au> wrote:
              [color=blue]
              >On Thu, 13 Nov 2003 06:26:09 GMT, Ron Adam wrote:[color=green]
              >> The only thing that surprises me in all of this is the "if var:"
              >> evaluating to true for numbers other than 1. That's new to me, I
              >> would have expected an exception in that case.[/color]
              >
              >Python has only recently gained a Boolean type ('bool').
              >
              > <http://www.python.org/peps/pep-0285.html>
              >
              >Before that, Boolean logic was done with integer values. Zero equated
              >to Boolean false, non-zero equated to Boolean true; and the default
              >Boolean true value was simply the integer 1.
              >
              >This conflation of types is confusing, and (like many other languages)
              >Python has now "grown a Boolean type" to distinguish integer 0 and 1
              >from Boolean False and True. However, the previous behaviour is still
              >supported -- for how long, I don't know.[/color]

              That's good to know. I've always explicitly defined my bool values as
              0 and 1 so it's never been a problem.

              Thanks for the PEP link, it was informative.

              _Ron Adam


              Comment

              • Michael Geary

                #8
                Re: True inconsistency in Python

                Ron Adam:[color=blue]
                > The only thing that surprises me in all of this is the
                > "if var:" evaluating to true for numbers other than 1.
                > That's new to me, I would have expected an
                > exception in that case. But it still makes since if I
                > look at it as a shortcut for "if (var != 0):". This only
                > proves I'm still relatively new to Python I think.[/color]

                It's handy, and natural in many real life situations, to treat any nonzero
                value as "true". For example:

                Do you have any money in your wallet?

                Do you have children?

                I could ask how much money or how many children you have, but if I just ask
                the yes-or-no question, you'll naturally answer "yes" for any nonzero value.

                Of course, if you have children, you won't have money, but that's a separate
                problem...

                -Mike


                Comment

                • Borcis

                  #9
                  Re: True inconsistency in Python

                  Scott Chapman wrote:
                  [color=blue]
                  >
                  > if var == True: # only works if var is 1
                  > blah
                  >
                  > ' Must use:
                  >
                  > if var: # works if var is not 0
                  > blah[/color]

                  there's the equivalent, and more explicit :

                  if bool(var)==True : blah

                  Comment

                  • Dang Griffith

                    #10
                    Re: True inconsistency in Python

                    On Wed, 12 Nov 2003 21:21:03 -0800, Erik Max Francis <max@alcyone.co m>
                    wrote:
                    [color=blue]
                    >Scott Chapman wrote:
                    >[color=green]
                    >> Historically Python has allowed <> 0 to equal true in evaluations.
                    >> Now
                    >> <> 0 still evaluates to true in evaluations. However it doesn't equal
                    >> True. They are not interchangable. (Same with empty lists, etc.)[/color]
                    >
                    >That's because the proper way to test for truth does not use the True
                    >value at all. It is this:
                    >
                    > if x:
                    > ...
                    >
                    >not this:
                    >
                    > if x == True:
                    > ...[/color]
                    Sort of like (stretchy) saying that if these are both True:
                    "a snake is green"
                    "a pearl is white"
                    that they are the same as each other.
                    They are both true, yet unrelated. You can say:
                    if "a snake is green": print 1
                    if "a pearl is white": print 2
                    and the prints would happen, but if you said
                    if "a snake is green" == "a pearl is white": print 3
                    the print would not happen.

                    --dang

                    Comment

                    • Alex Martelli

                      #11
                      Re: True inconsistency in Python

                      Borcis wrote:
                      [color=blue]
                      > Scott Chapman wrote:
                      >[color=green]
                      >>
                      >> if var == True: # only works if var is 1
                      >> blah
                      >>
                      >> ' Must use:
                      >>
                      >> if var: # works if var is not 0
                      >> blah[/color]
                      >
                      > there's the equivalent, and more explicit :
                      >
                      > if bool(var)==True : blah[/color]

                      Why stop there? If adding one useless and redundant check is
                      better, surely having more will be merrier and merrier...:

                      if ( bool(var) == True) == True: doubleblah

                      oh wait, we should add ANOTHER level of uselessness...:

                      if (( bool(var) == True) == True) == True: tripleblah

                      oh wait...


                      "if var:" is the Pythonic way. You could argue that each
                      level of "== True" makes it ``more explicit'', but I just
                      consider them all equally silly in their utter redundancy.

                      [[ we TOLD Guido people would start on this absurd path, when
                      he added bool, True, and False, but he wouldn't listen...
                      ]]


                      Alex

                      Comment

                      • Alex Martelli

                        #12
                        Re: True inconsistency in Python

                        Ben Finney wrote:
                        ...[color=blue]
                        > This conflation of types is confusing, and (like many other languages)
                        > Python has now "grown a Boolean type" to distinguish integer 0 and 1
                        > from Boolean False and True. However, the previous behaviour is still
                        > supported -- for how long, I don't know.[/color]

                        I've never heard of any plans (not even for the totally mythical
                        "Python 3000") to remove or even deprecate Python's extremely useful
                        and practical feature that:
                        -- any value can be used as a true-or-false condition,
                        -- zero numbers, empty containers, and user-coded objects defining
                        __nonzero__ (or else __len__) and returning 0 are taken as false,
                        -- all other values are taken as true.


                        Alex

                        Comment

                        • Emile van Sebille

                          #13
                          Re: True inconsistency in Python

                          Ben Finney[color=blue]
                          > You many want to read the PEP that led to the creation of the 'bool'
                          > type (and True and False objects):[/color]

                          .... or vise versa

                          Emile van Sebille
                          emile@fenx.com


                          Comment

                          • Alex Martelli

                            #14
                            Re: True inconsistency in Python

                            Scott Chapman wrote:
                            ...[color=blue]
                            > It seems that maybe Python should throw a warning (perhaps if a flag is
                            > set) any time it bumps into code comparing a variable to True or False.[/color]

                            It's a bit hard to do it just for variables, but doing it for _any_
                            comparison would _almost_ be reasonable -- testing if something
                            "== True" is hardly ever sensible. Unfortunately "a == b" CAN
                            be perfectly sensible and either or both of the variables MIGHT
                            just happen to be set to True.

                            However, this looks to me like a good suggestion for PyChecker
                            and the like, which should be able to catch and flag the actual
                            explicit use or the constant True in code involving == or != ...


                            Alex

                            Comment

                            • Gerrit Holl

                              #15
                              Re: True inconsistency in Python

                              Alex Martelli wrote:
                              (about x vs. bool(x))[color=blue]
                              > [[ we TOLD Guido people would start on this absurd path, when
                              > he added bool, True, and False, but he wouldn't listen...
                              > ]][/color]

                              What do double square brackets mean?

                              (btw, I _do_ use bool ocasionally: I implemented an anytrue() function
                              using sum([bool(i) for i in L]), is this wrong?)

                              Gerrit.

                              --
                              128. If a man take a woman to wife, but have no intercourse with her,
                              this woman is no wife to him.
                              -- 1780 BC, Hammurabi, Code of Law
                              --
                              Asperger Syndroom - een persoonlijke benadering:

                              Kom in verzet tegen dit kabinet:
                              De website van de Socialistische Partij (SP) in Nederland: Informatie, nieuws, agenda en publicaties.


                              Comment

                              Working...