Comparisons and singletons

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steven Watanabe

    #1

    Comparisons and singletons

    PEP 8 says, "Comparison s to singletons like None should always be done
    with 'is' or 'is not', never the equality operators." I know that "is"
    is an identity operator, "==" and "!=" are the equality operators, but
    I'm not sure what other singletons are being referred to here.

    Also, I've seen code that does things like:

    if foo is 3:
    if foo is not '':

    Are these valid uses of "is"?

    Thanks in advance.
    --
    Steven.
  • Ziga Seilnacht

    #2
    Re: Comparisons and singletons

    Steven Watanabe wrote:[color=blue]
    > PEP 8 says, "Comparison s to singletons like None should always be done
    > with 'is' or 'is not', never the equality operators." I know that "is"
    > is an identity operator, "==" and "!=" are the equality operators, but
    > I'm not sure what other singletons are being referred to here.[/color]

    Other builtin singeltons are NotImplemented and Ellipsis, see:

    for details.
    [color=blue]
    >
    > Also, I've seen code that does things like:
    >
    > if foo is 3:
    > if foo is not '':
    >
    > Are these valid uses of "is"?[/color]

    No. Try this examples:
    [color=blue][color=green][color=darkred]
    >>> a = 'spam'
    >>> b = ''.join(list(a) )
    >>> b[/color][/color][/color]
    'spam'[color=blue][color=green][color=darkred]
    >>> a == b[/color][/color][/color]
    True[color=blue][color=green][color=darkred]
    >>> a is b[/color][/color][/color]
    False[color=blue][color=green][color=darkred]
    >>> a = 10000
    >>> b = 10000
    >>> a == b[/color][/color][/color]
    True[color=blue][color=green][color=darkred]
    >>> a is b[/color][/color][/color]
    False
    [color=blue]
    >
    > Thanks in advance.
    > --
    > Steven.[/color]

    Hope this helps.

    Ziga

    Comment

    • David Isaac

      #3
      Re: Comparisons and singletons

      "Ziga Seilnacht" <ziga.seilnacht @gmail.com> wrote in message
      news:1143283477 .483035.212980@ v46g2000cwv.goo glegroups.com.. .[color=blue][color=green][color=darkred]
      > >>> a = 10000
      > >>> b = 10000
      > >>> a == b[/color][/color]
      > True[color=green][color=darkred]
      > >>> a is b[/color][/color]
      > False[/color]

      Two follow up questions:

      1. I wondered about your example,
      and noticed[color=blue][color=green][color=darkred]
      >>> a = 10
      >>> b = 10
      >>> a is b[/color][/color][/color]
      True

      Why the difference?

      2. If I really want a value True will I ever go astray with the test:
      if a is True:[color=blue][color=green][color=darkred]
      >>> a = True
      >>> b = 1.
      >>> c = 1
      >>> a is True, b is True, c is True[/color][/color][/color]
      (True, False, False)

      Thanks,
      Alan Isaac


      Comment

      • Chris Mellon

        #4
        Re: Comparisons and singletons

        On 3/25/06, David Isaac <aisaac0@verizo n.net> wrote:[color=blue]
        > "Ziga Seilnacht" <ziga.seilnacht @gmail.com> wrote in message
        > news:1143283477 .483035.212980@ v46g2000cwv.goo glegroups.com.. .[color=green][color=darkred]
        > > >>> a = 10000
        > > >>> b = 10000
        > > >>> a == b[/color]
        > > True[color=darkred]
        > > >>> a is b[/color]
        > > False[/color]
        >
        > Two follow up questions:
        >
        > 1. I wondered about your example,
        > and noticed[color=green][color=darkred]
        > >>> a = 10
        > >>> b = 10
        > >>> a is b[/color][/color]
        > True
        >
        > Why the difference?
        >
        > 2. If I really want a value True will I ever go astray with the test:
        > if a is True:[color=green][color=darkred]
        > >>> a = True
        > >>> b = 1.
        > >>> c = 1
        > >>> a is True, b is True, c is True[/color][/color]
        > (True, False, False)
        >[/color]

        None, True, and False are all singletons and should be compared with
        "is". There are some other singletons - small integers (up to 10, I
        believe) as well as the empty string. However, I am not sure (and I am
        sure someone will correct me if I'm wrong) but I believe that these
        are not specified as singletons, and that it's an implementation
        detail that they are.
        [color=blue]
        > Thanks,
        > Alan Isaac
        >
        >
        > --
        > http://mail.python.org/mailman/listinfo/python-list
        >[/color]

        Comment

        • Ziga Seilnacht

          #5
          Re: Comparisons and singletons

          David Isaac wrote:[color=blue]
          > "Ziga Seilnacht" <ziga.seilnacht @gmail.com> wrote in message
          > news:1143283477 .483035.212980@ v46g2000cwv.goo glegroups.com.. .[color=green][color=darkred]
          > > >>> a = 10000
          > > >>> b = 10000
          > > >>> a == b[/color]
          > > True[color=darkred]
          > > >>> a is b[/color]
          > > False[/color]
          >
          > Two follow up questions:
          >
          > 1. I wondered about your example,
          > and noticed[color=green][color=darkred]
          > >>> a = 10
          > >>> b = 10
          > >>> a is b[/color][/color]
          > True
          >
          > Why the difference?[/color]

          Python has a special internal list of integers in which it caches
          numbers smaller than 1000 (I'm not sure that the number is correct),
          but that is an implementation detail and you should not rely on it.
          [color=blue]
          > 2. If I really want a value True will I ever go astray with the test:
          > if a is True:[color=green][color=darkred]
          > >>> a = True
          > >>> b = 1.
          > >>> c = 1
          > >>> a is True, b is True, c is True[/color][/color]
          > (True, False, False)[/color]

          I think that True and False, although they were added in version
          2.3, were not true singeltons until version 2.4. You should finish
          reading the PEP, see especially this part:

          - Don't compare boolean values to True or False using ==

          Yes: if greeting:

          No: if greeting == True:

          Worse: if greeting is True:
          [color=blue]
          >
          > Thanks,
          > Alan Isaac[/color]

          Ziga

          Comment

          • Felipe Almeida Lessa

            #6
            Re: Comparisons and singletons

            Em Sáb, 2006-03-25 às 09:11 -0800, Ziga Seilnacht escreveu:[color=blue]
            > Python has a special internal list of integers in which it caches
            > numbers smaller than 1000 (I'm not sure that the number is correct),
            > but that is an implementation detail and you should not rely on it.[/color]

            By testing:[color=blue][color=green][color=darkred]
            >>> a = 10
            >>> b = 10
            >>> a is b[/color][/color][/color]
            True[color=blue][color=green][color=darkred]
            >>> a = 100
            >>> b = 100
            >>> a is b[/color][/color][/color]
            False[color=blue][color=green][color=darkred]
            >>> a = 50
            >>> b = 50
            >>> a is b[/color][/color][/color]
            True[color=blue][color=green][color=darkred]
            >>> a = 70
            >>> b = 70
            >>> a is b[/color][/color][/color]
            True[color=blue][color=green][color=darkred]
            >>> a = 99
            >>> b = 99
            >>> a is b[/color][/color][/color]
            True

            And to the other side:
            [color=blue][color=green][color=darkred]
            >>> a = -10
            >>> b = -10
            >>> a is b[/color][/color][/color]
            False[color=blue][color=green][color=darkred]
            >>> a = -5
            >>> b = -5
            >>> a is b[/color][/color][/color]
            True[color=blue][color=green][color=darkred]
            >>> a = -6
            >>> b = -6
            >>> a is b[/color][/color][/color]
            False

            And then, when looking to Python 2.4's code[1]:
            """
            #ifndef NSMALLPOSINTS
            #define NSMALLPOSINTS 100
            #endif
            #ifndef NSMALLNEGINTS
            #define NSMALLNEGINTS 5
            #endif
            #if NSMALLNEGINTS + NSMALLPOSINTS > 0
            /* References to small integers are saved in this array so that they
            can be shared.
            The integers that are saved are those in the range
            -NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive).
            */
            static PyIntObject *small_ints[NSMALLNEGINTS + NSMALLPOSINTS];
            #endif
            """

            However, as stated before, don't rely on these numbers. The trunk[2] defines now 256, not 99, as the biggest integer shared.

            [1]

            [2] http://svn.python.org/projects/pytho...ts/intobject.c

            HTH,

            --
            Felipe.

            Comment

            • David Isaac

              #7
              Re: Comparisons and singletons

              Alan asked:[color=blue][color=green]
              > > 2. If I really want a value True will I ever go astray with the test:
              > > if a is True:[color=darkred]
              > > >>> a = True
              > > >>> b = 1.
              > > >>> c = 1
              > > >>> a is True, b is True, c is True[/color]
              > > (True, False, False)[/color][/color]

              "Ziga Seilnacht" <ziga.seilnacht @gmail.com> wrote in message
              news:1143306711 .041441.43470@i 40g2000cwc.goog legroups.com...[color=blue]
              > I think that True and False, although they were added in version
              > 2.3, were not true singeltons until version 2.4.[/color]

              OK, but Python 2.3 yields the same result as above.

              Ziga wrote:[color=blue]
              > You should finish
              > reading the PEP, see especially this part:
              > - Don't compare boolean values to True or False using ==
              > Yes: if greeting:
              > No: if greeting == True:
              > Worse: if greeting is True:[/color]


              I do not think this is relevant to the question I asked,
              which was how to test for a value of True, if that's
              what I really want. I think the outcome of this
              discussion has been: use 'is'.

              Thanks,
              Alan


              Comment

              Working...