'Using is not None, may not always work'

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Doug Fort

    'Using is not None, may not always work'

    Hi,

    Since I installed 2.4a2 I've been getting a warning from pychecker: Using
    is not None, may not always work'. I thought 'is not None' was the right
    thing to do. I've had problems with 'if not x:', because some objects
    return False in this context.

    --
    Doug Fort, Consulting Programmer


  • Heiko Wundram

    #2
    Re: 'Using is not None, may not always work'

    Am Freitag, 6. August 2004 15:15 schrieb Doug Fort:[color=blue]
    > Since I installed 2.4a2 I've been getting a warning from pychecker: Using
    > is not None, may not always work'. I thought 'is not None' was the right
    > thing to do. I've had problems with 'if not x:', because some objects
    > return False in this context.[/color]

    That's probably what pychecker warns you about: that you might get an object
    in the respective context which evaluates to boolean false, but is not
    None... Although I'd find this strange...

    Heiko.

    Comment

    • Michael Hudson

      #3
      Re: 'Using is not None, may not always work'

      Heiko Wundram <heikowu@ceosg. de> writes:
      [color=blue]
      > Am Freitag, 6. August 2004 15:15 schrieb Doug Fort:[color=green]
      > > Since I installed 2.4a2 I've been getting a warning from pychecker: Using
      > > is not None, may not always work'. I thought 'is not None' was the right
      > > thing to do. I've had problems with 'if not x:', because some objects
      > > return False in this context.[/color]
      >
      > That's probably what pychecker warns you about: that you might get an object
      > in the respective context which evaluates to boolean false, but is not
      > None... Although I'd find this strange...[/color]

      Does seem an odd warning, seeing as when I write "x is not None" I'm
      generally avoiding the case of, e.g., x being the empty list.

      Cheers,
      mwh

      --
      Well, you pretty much need Microsoft stuff to get misbehaviours
      bad enough to actually tear the time-space continuum. Luckily
      for you, MS Internet Explorer is available for Solaris.
      -- Calle Dybedahl, alt.sysadmin.re covery

      Comment

      • Grant Edwards

        #4
        Re: 'Using is not None, may not always work'

        On 2004-08-06, Doug Fort <dougfort@dougf ort.com> wrote:
        [color=blue]
        > Since I installed 2.4a2 I've been getting a warning from
        > pychecker: Using is not None, may not always work'.[/color]

        Huh?!

        That implies that there are cases where "None is not None" will
        evaluate as True. I find that extremely hard to believe.
        [color=blue]
        > I thought 'is not None' was the right thing to do.[/color]

        It's certainly what I want to do if I want to know if a name is
        bound to None or not.
        [color=blue]
        > I've had problems with 'if not x:', because some objects
        > return False in this context.[/color]

        Exactly.

        --
        Grant Edwards grante Yow! I was in a HOT
        at TUB! I was NORMAL! I was
        visi.com ITALIAN!! I enjoyed th'
        EARTHQUAKE!

        Comment

        • Peter Otten

          #5
          Re: 'Using is not None, may not always work'

          Doug Fort wrote:
          [color=blue]
          > Since I installed 2.4a2 I've been getting a warning from pychecker: Using
          > is not None, may not always work'. I thought 'is not None' was the right
          > thing to do. I've had problems with 'if not x:', because some objects
          > return False in this context.[/color]

          This is harmless. Starting with 2.4a2 None is a constant:

          Python 2.3.3 (#1, Jan 3 2004, 13:57:08)
          [GCC 3.2] on linux2
          Type "help", "copyright" , "credits" or "license" for more information.[color=blue][color=green][color=darkred]
          >>> import dis
          >>> def f():[/color][/color][/color]
          .... if x is None: pass
          ....[color=blue][color=green][color=darkred]
          >>> dis.dis(f)[/color][/color][/color]
          2 0 LOAD_GLOBAL 0 (x)
          3 LOAD_GLOBAL 1 (None)
          6 COMPARE_OP 8 (is)
          9 JUMP_IF_FALSE 4 (to 16)
          12 POP_TOP
          13 JUMP_FORWARD 1 (to 17)[color=blue][color=green]
          >> 16 POP_TOP
          >> 17 LOAD_CONST 0 (None)[/color][/color]
          20 RETURN_VALUE[color=blue][color=green][color=darkred]
          >>>[/color][/color][/color]

          Python 2.4a2 (#1, Aug 6 2004, 16:38:38)
          [GCC 3.2] on linux2
          Type "help", "copyright" , "credits" or "license" for more information.
          [python 2.4a2][color=blue][color=green][color=darkred]
          >>> import dis
          >>> def f():[/color][/color][/color]
          .... if x is None: pass
          ....[color=blue][color=green][color=darkred]
          >>> dis.dis(f)[/color][/color][/color]
          2 0 LOAD_GLOBAL 0 (x)
          3 LOAD_CONST 0 (None)
          6 COMPARE_OP 8 (is)
          9 JUMP_IF_FALSE 4 (to 16)
          12 POP_TOP
          13 JUMP_FORWARD 1 (to 17)[color=blue][color=green]
          >> 16 POP_TOP
          >> 17 LOAD_CONST 0 (None)[/color][/color]
          20 RETURN_VALUE


          When PyChecker sees the constant it supposes you are doing something like

          if x is "some string": pass

          which always had the LOAD_CONST op-code and is indeed dangerous. PyChecker
          needs to be fixed to special-case None for 2.4.

          Peter


          Comment

          • Doug Fort

            #6
            Re: 'Using is not None, may not always work'

            On Fri, 06 Aug 2004 17:02:43 +0200, Peter Otten wrote:
            [color=blue]
            > Doug Fort wrote:
            >[color=green]
            >> Since I installed 2.4a2 I've been getting a warning from pychecker: Using
            >> is not None, may not always work'. I thought 'is not None' was the right
            >> thing to do. I've had problems with 'if not x:', because some objects
            >> return False in this context.[/color]
            >
            > This is harmless. Starting with 2.4a2 None is a constant:[/color]

            Thanks, that it.
            [color=blue]
            >
            > Python 2.3.3 (#1, Jan 3 2004, 13:57:08)
            > [GCC 3.2] on linux2
            > Type "help", "copyright" , "credits" or "license" for more information.[color=green][color=darkred]
            >>>> import dis
            >>>> def f():[/color][/color]
            > ... if x is None: pass
            > ...[color=green][color=darkred]
            >>>> dis.dis(f)[/color][/color]
            > 2 0 LOAD_GLOBAL 0 (x)
            > 3 LOAD_GLOBAL 1 (None)
            > 6 COMPARE_OP 8 (is)
            > 9 JUMP_IF_FALSE 4 (to 16)
            > 12 POP_TOP
            > 13 JUMP_FORWARD 1 (to 17)[color=green][color=darkred]
            > >> 16 POP_TOP
            > >> 17 LOAD_CONST 0 (None)[/color][/color]
            > 20 RETURN_VALUE[color=green][color=darkred]
            >>>>[/color][/color]
            >
            > Python 2.4a2 (#1, Aug 6 2004, 16:38:38)
            > [GCC 3.2] on linux2
            > Type "help", "copyright" , "credits" or "license" for more information.
            > [python 2.4a2][color=green][color=darkred]
            >>>> import dis
            >>>> def f():[/color][/color]
            > ... if x is None: pass
            > ...[color=green][color=darkred]
            >>>> dis.dis(f)[/color][/color]
            > 2 0 LOAD_GLOBAL 0 (x)
            > 3 LOAD_CONST 0 (None)
            > 6 COMPARE_OP 8 (is)
            > 9 JUMP_IF_FALSE 4 (to 16)
            > 12 POP_TOP
            > 13 JUMP_FORWARD 1 (to 17)[color=green][color=darkred]
            > >> 16 POP_TOP
            > >> 17 LOAD_CONST 0 (None)[/color][/color]
            > 20 RETURN_VALUE
            >
            >
            > When PyChecker sees the constant it supposes you are doing something like
            >
            > if x is "some string": pass
            >
            > which always had the LOAD_CONST op-code and is indeed dangerous. PyChecker
            > needs to be fixed to special-case None for 2.4.
            >
            > Peter[/color]

            --
            Doug Fort, Consulting Programmer


            Comment

            Working...