'Using is not None, may not always work'

Doug Fort dougfort at dougfort.com
Fri Aug 6 11:06:29 EDT 2004


On Fri, 06 Aug 2004 17:02:43 +0200, Peter Otten wrote:

> Doug Fort wrote:
> 
>> 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.
>  
> This is harmless. Starting with 2.4a2 None is a constant:

Thanks, that it.

> 
> 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.
>>>> import dis
>>>> def f():
> ...     if x is None: pass
> ...
>>>> dis.dis(f)
>   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)
>         >>   16 POP_TOP
>         >>   17 LOAD_CONST               0 (None)
>              20 RETURN_VALUE
>>>>
> 
> 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]
>>>> import dis
>>>> def f():
> ...     if x is None: pass
> ...
>>>> dis.dis(f)
>   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)
>         >>   16 POP_TOP
>         >>   17 LOAD_CONST               0 (None)
>              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

-- 
Doug Fort, Consulting Programmer
http://www.dougfort.com




More information about the Python-list mailing list