True

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Daniel Klein

    True

    In Python 2.2 I use to have

    true = (1 == 1)
    false = not true

    This was at the recommendation of someone on this list some time ago.
    The reason (if I remember correctly) was that setting

    true = 1
    false = 0

    were not true booleans.

    Now the expression (1 == 1) returns 'True', and caused a bug in my
    code. So my question is what is the proper method for setting booleans
    in 2.3?

    Really confused,

    Daniel Klein
  • Lee Harr

    #2
    Re: True

    In article <fucrivcvd2mel7 k51ij1hktb55gqn mk9ic@4ax.com>, Daniel Klein wrote:[color=blue]
    > In Python 2.2 I use to have
    >
    > true = (1 == 1)
    > false = not true
    >
    > This was at the recommendation of someone on this list some time ago.
    > The reason (if I remember correctly) was that setting
    >
    > true = 1
    > false = 0
    >
    > were not true booleans.
    >
    > Now the expression (1 == 1) returns 'True', and caused a bug in my[/color]

    Actually, it returns True, not 'True'
    [color=blue]
    > code. So my question is what is the proper method for setting booleans
    > in 2.3?
    >
    > Really confused,
    >[/color]

    True and False are now built in to 2.3
    [color=blue]
    >python[/color]
    Python 2.3 (#1, Aug 1 2003, 15:18:54)
    [GCC 2.95.4 20020320 [FreeBSD]] on freebsd4
    Type "help", "copyright" , "credits" or "license" for more information.[color=blue][color=green][color=darkred]
    >>> dir(__builtins_ _)[/color][/color][/color]
    ['ArithmeticErro r', 'AssertionError ', 'AttributeError ', 'DeprecationWar ning',
    'EOFError', 'Ellipsis', 'EnvironmentErr or', 'Exception',

    'False', .snip. 'True', .snip.

    'type', 'unichr', 'unicode', 'vars', 'xrange', 'zip']



    but I think if you had an up-to-date 2.2 they should have been there too...



    [color=blue]
    >python2.2[/color]
    Python 2.2.3 (#1, Jun 9 2003, 18:01:50)
    [GCC 2.95.4 20020320 [FreeBSD]] on freebsd4
    Type "help", "copyright" , "credits" or "license" for more information.[color=blue][color=green][color=darkred]
    >>> dir(__builtins_ _)[/color][/color][/color]
    ['ArithmeticErro r', 'AssertionError ', 'AttributeError ', 'DeprecationWar ning',
    'EOFError', 'Ellipsis', 'EnvironmentErr or', 'Exception',

    'False', .snip. 'True', .snip.

    'super', 'tuple', 'type', 'unichr', 'unicode', 'vars', 'xrange', 'zip']




    What error are you getting? How are you using true and false?
    I know the developers were very careful to not cause any compatibilty
    problems with the addition of booleans.

    Comment

    • John Roth

      #3
      Re: True


      "Daniel Klein" <danielk@aracne t.com> wrote in message
      news:fucrivcvd2 mel7k51ij1hktb5 5gqnmk9ic@4ax.c om...[color=blue]
      > In Python 2.2 I use to have
      >
      > true = (1 == 1)
      > false = not true
      >
      > This was at the recommendation of someone on this list some time ago.
      > The reason (if I remember correctly) was that setting
      >
      > true = 1
      > false = 0
      >
      > were not true booleans.[/color]

      There were no true booleans in 2.2 and earlier. Whoever recommended
      that didn't know what he was talking about. There was no difference.
      [color=blue]
      > Now the expression (1 == 1) returns 'True', and caused a bug in my
      > code. So my question is what is the proper method for setting booleans
      > in 2.3?[/color]

      I presume what broke your code was depending on the return from
      either str() or repr(), or the % operator. That was, unfortunately, one
      of the incompatibilite s between 2.2 and 2.3.

      In 2.3, Boolean is a subtype of Int, and has two values: True and False.
      Both of these are built in constants, so just use them. That's what they're
      for. For most purposes, the are the same as 1 and 0, except for what they
      return from str() and repr(), and how they get formatted with %.

      John Roth
      [color=blue]
      >
      > Really confused,
      >
      > Daniel Klein[/color]


      Comment

      • John Roth

        #4
        Re: True


        "Andrew Dalke" <adalke@mindspr ing.com> wrote in message
        news:bgknba$fk1 $1@slb0.atl.min dspring.net...[color=blue]
        > Daniel Klein:[color=green][color=darkred]
        > > > true = (1 == 1)
        > > > false = not true
        > > >
        > > > This was at the recommendation of someone on this list some time ago.[/color][/color]
        >
        > John Roth[color=green]
        > > There were no true booleans in 2.2 and earlier. Whoever recommended
        > > that didn't know what he was talking about. There was no difference.[/color]
        >
        > Indeed, there were no true booleans in Python, but there were
        > actual PyTrue and PyFalse objects at the C level. Doing the "1 == 1"
        > trick would expose that implementation choice to Python.
        >
        > This was rarely used. The only place I know of is in the win32 code,
        > where a COM API can take a boolean or an integer function. The
        > win32 interface checks if the value is PyTrue/PyFalse,
        >
        > Here's relevant links
        > http://mail.python.org/pipermail/pyt...er/015260.html
        > http://mailman.pythonpros.com/piperm...q1/000106.html[/color]

        So, are you saying that this would have returned -1, rather than 1?

        John Roth
        [color=blue]
        > Andrew
        > dalke@dalkescie ntific.com
        >
        >[/color]


        Comment

        • François Pinard

          #5
          Re: True

          [Daniel Klein]
          [color=blue]
          > So my question is what is the proper method for setting booleans in 2.3?[/color]

          Hi, Daniel.

          In 2.3, you do not need to set booleans, `True' and `False' are just there.

          However, if you have a need to write portably against many versions, you
          might try something like:

          try:
          True
          except NameError:
          False, True = range(2)

          in modules where you need to use `True' or `False'.

          --
          François Pinard http://www.iro.umontreal.ca/~pinard

          Comment

          • Andrew Dalke

            #6
            Re: True

            John Roth:[color=blue]
            > So, are you saying that this would have returned -1, rather than 1?[/color]

            No, and I don't know how you thought I suggested it. If PyTrue returned
            -1 before then "True = (1==1)" would not provide the right compatibility
            between different Python versions and so would not be an appropriate
            suggestion at all.

            You must have misread the text from the second URL

            in it, Mark Hammond said:[color=blue]
            > The only thing that I can see changing is the literal value of the
            > boolean field - currently, the conversion from VT_BOOL to integer will
            > result in 0 or -1. PyTrue is 1[/color]

            The boolean returned from COM is -1. PyTrue is (and was) 1.

            Andrew
            dalke@dalkescie ntific.com


            Comment

            • Daniel Klein

              #7
              Re: True

              On Mon, 04 Aug 2003 02:13:57 GMT, Lee Harr <missive@fronti ernet.net>
              wrote:
              [color=blue]
              >In article <fucrivcvd2mel7 k51ij1hktb55gqn mk9ic@4ax.com>, Daniel Klein wrote:[color=green]
              >> In Python 2.2 I use to have
              >>
              >> true = (1 == 1)
              >> false = not true
              >>
              >> This was at the recommendation of someone on this list some time ago.
              >> The reason (if I remember correctly) was that setting
              >>
              >> true = 1
              >> false = 0
              >>
              >> were not true booleans.
              >>
              >> Now the expression (1 == 1) returns 'True', and caused a bug in my[/color]
              >
              >Actually, it returns True, not 'True'
              >[/color]

              In this case, it _did_ return a literal string of 'True' cuz I was
              using str(true) and str(false). That was the nature of the buglet.
              Before str(true) would return return '1' and str(false) would return
              '0'. Sorry that I didn't include this bit of information in the
              original post :-(

              Dan

              Comment

              • Daniel Klein

                #8
                Re: True

                On 04 Aug 2003 06:17:37 -0400, pinard@iro.umon treal.ca (François
                Pinard) wrote:
                [color=blue]
                >[Daniel Klein]
                >[color=green]
                >> So my question is what is the proper method for setting booleans in 2.3?[/color]
                >
                >Hi, Daniel.
                >
                >In 2.3, you do not need to set booleans, `True' and `False' are just there.
                >
                >However, if you have a need to write portably against many versions, you
                >might try something like:
                >
                > try:
                > True
                > except NameError:
                > False, True = range(2)
                >
                >in modules where you need to use `True' or `False'.[/color]

                Thanks François. I don't need to support multiple versions. I simply
                changed the code from:

                true = (1 == 1)

                to

                true = 1

                and presto, no more bug :-)

                Dan

                Comment

                • Daniel Klein

                  #9
                  Re: True

                  On Wed, 06 Aug 2003 09:18:13 +1000, Mark Hammond
                  <mhammond@skipp inet.com.au> wrote:
                  [color=blue]
                  >Daniel Klein wrote:[color=green]
                  >> Thanks François. I don't need to support multiple versions. I simply
                  >> changed the code from:
                  >>
                  >> true = (1 == 1)
                  >>
                  >> to
                  >>
                  >> true = 1
                  >>
                  >> and presto, no more bug :-)[/color]
                  >
                  >For the sake of being pedantic, I believe your code still does have a
                  >bug. You bug is that you rely on the "str()" of a boolean value
                  >returning a specific string. Your code should probably check the bool
                  >and create your own string rather than relying on this behaviour
                  >remaining unchanged forever.[/color]

                  I'm not really taking a str() on a boolean value anymore; it's being
                  taken on an integer value. Are you saying that...

                  true = 1
                  str(true) # this should always return '1' now and forever more

                  ....could cause problems in the future?

                  The reason I'm taking a str() value is cuz it is being sent over a
                  socket to a non-python server process, where it is converted back to a
                  boolean.

                  Make sense now?

                  Dan

                  Comment

                  • Daniel Klein

                    #10
                    Re: True

                    On Wed, 06 Aug 2003 12:18:45 -0400, Peter Hansen <peter@engcorp. com>
                    wrote:
                    [color=blue][color=green]
                    >> The reason I'm taking a str() value is cuz it is being sent over a
                    >> socket to a non-python server process, where it is converted back to a
                    >> boolean.[/color]
                    >
                    >In that case, you'd be better off converting using your own routine
                    >rather than relying on a particular behaviour of str() for this test.
                    >Basically, do something like
                    >
                    >def boolean2String( boolVal):
                    > return { False : '1', True : '0' } [ not boolVal ]
                    >
                    >and save yourself any further future pain...[/color]

                    Thanks Peter, that will do the trick. Just wondering though why you
                    chose to code the opposite values and not as

                    def boolean2String( boolVal):
                    return {True:'1', False:'0'}[boolVal]

                    Dan

                    Comment

                    • Terry Reedy

                      #11
                      Re: True


                      "Daniel Klein" <danielk@aracne t.com> wrote in message
                      news:vrh4jvopu2 9elj8fu26upnf48 f45de22h9@4ax.c om...
                      [color=blue]
                      > On Wed, 06 Aug 2003 12:18:45 -0400, Peter Hansen <peter@engcorp. com>
                      > wrote:[color=green]
                      > >def boolean2String( boolVal):
                      > > return { False : '1', True : '0' } [ not boolVal ][/color][/color]
                      [color=blue]
                      > Thanks Peter, that will do the trick. Just wondering though why you
                      > chose to code the opposite values and not as
                      >
                      > def boolean2String( boolVal):
                      > return {True:'1', False:'0'}[boolVal][/color]

                      Your function requires the arg to be 0/1 (True/False). Peter's
                      function works for *any* input that can be 'not'ed.

                      TJR


                      Comment

                      • Martin v. Löwis

                        #12
                        Re: True

                        Peter Hansen <peter@engcorp. com> writes:
                        [color=blue]
                        > As Skip said... but it should have had a comment anyway, since it
                        > wasn't clear. You could also consider using "not not boolVal" if
                        > you want to make the code _slightly_ (IMHO) more readable, and slightly
                        > slower, but I think one still needs a comment explaining it. :-([/color]

                        I find

                        def boolean2str(val ):
                        if val:
                        return '1'
                        else:
                        return '0'

                        both more readable, and twice as fast. It doesn't need to create a
                        dictionary each time, and it does not to perform a dictionary lookup.
                        If you prefer compactness, use

                        def boolean2str(val , results=('1', '0')):
                        return results[not val]

                        Regards,
                        Martin

                        Comment

                        • Terry Reedy

                          #13
                          Re: True


                          "Martin v. Löwis" <martin@v.loewi s.de> wrote in message
                          news:m34r0tmmcm .fsf@mira.infor matik.hu-berlin.de...[color=blue]
                          > If you prefer compactness, use
                          >
                          > def boolean2str(val , results=('1', '0')):
                          > return results[not val][/color]

                          -or, more safely (no default arg to accidently overwrite)-

                          def boolean2str(val ):
                          return val and '1' or '0'

                          But one might want the flexibility of intentionally replacing the
                          results default, as in

                          print boolean2str(som evalue, ('T', 'F'))

                          Terry J. Reedy




                          Comment

                          • Paul Watson

                            #14
                            Re: True


                            "John Roth" <newsgroups@jhr othjr.com> wrote in message
                            news:virjpa532l 4117@news.super news.com...[color=blue]
                            >
                            > "Daniel Klein" <danielk@aracne t.com> wrote in message
                            > news:fucrivcvd2 mel7k51ij1hktb5 5gqnmk9ic@4ax.c om...[color=green]
                            > > In Python 2.2 I use to have
                            > >
                            > > true = (1 == 1)
                            > > false = not true
                            > >
                            > > This was at the recommendation of someone on this list some time ago.
                            > > The reason (if I remember correctly) was that setting
                            > >
                            > > true = 1
                            > > false = 0
                            > >
                            > > were not true booleans.[/color]
                            >
                            > There were no true booleans in 2.2 and earlier. Whoever recommended
                            > that didn't know what he was talking about. There was no difference.
                            >[color=green]
                            > > Now the expression (1 == 1) returns 'True', and caused a bug in my
                            > > code. So my question is what is the proper method for setting booleans
                            > > in 2.3?[/color]
                            >
                            > I presume what broke your code was depending on the return from
                            > either str() or repr(), or the % operator. That was, unfortunately, one
                            > of the incompatibilite s between 2.2 and 2.3.
                            >
                            > In 2.3, Boolean is a subtype of Int, and has two values: True and False.
                            > Both of these are built in constants, so just use them. That's what[/color]
                            they're[color=blue]
                            > for. For most purposes, the are the same as 1 and 0, except for what they
                            > return from str() and repr(), and how they get formatted with %.
                            >
                            > John Roth
                            >[color=green]
                            > >
                            > > Really confused,
                            > >
                            > > Daniel Klein[/color][/color]

                            Then, what is the best way to write boolean operations for Python 2.1 so
                            that it will be as 2.3+ ready as possible?

                            Should we just use 0 and 1?

                            Until the vendor of the tool we are using delivers a more recent version of
                            Python with the product, we must produce 2.1 compatible code.

                            Thanks.


                            Comment

                            • David Eppstein

                              #15
                              Re: True

                              In article <3f929cf0$1_1@t hemost.net>,
                              "Paul Watson" <pwatson@redlin ec.com> wrote:
                              [color=blue]
                              > Then, what is the best way to write boolean operations for Python 2.1 so
                              > that it will be as 2.3+ ready as possible?[/color]

                              I've been including the following at the start of some of my code:

                              if 'True' not in globals():
                              globals()['True'] = not None
                              globals()['False'] = not True

                              My hope is that setting up True and False in this convoluted way will
                              allow it to continue to work in some future version where assignment to
                              builtins is disallowed.

                              --
                              David Eppstein http://www.ics.uci.edu/~eppstein/
                              Univ. of California, Irvine, School of Information & Computer Science

                              Comment

                              Working...