[issue46941] Bug or plug not removed (The operator "is")

Raymond Hettinger report at bugs.python.org
Sun Mar 6 20:21:16 EST 2022


Raymond Hettinger <raymond.hettinger at gmail.com> added the comment:

The code in the screenshot looks correct.



>>> i = 0
>>> i is int    
False
>>> type(i) is int
True

The code above does not get warning because "int" is a variable.  This kind of comparison is always allowed and will work reliably.



>>> i is 0
<stdin>:1: SyntaxWarning: "is" with a literal. Did you mean "=="?
True

The above code generates a warning because 0 is a numeric literal and there may be more than one instance of that literal.  While this kind of comparison is allowed, it is unreliable because numeric literals are not guaranteed to be singletons:

    >>> x = 600
    >>> (x + x) is 1200
    <stdin>:1: SyntaxWarning: "is" with a literal. Did you mean "=="?
    False



The reliable and correct way to test numeric values with an equality:

    >>> x + x == 1200
    True

----------
nosy: +rhettinger -Dennis Sweeney, Jelle Zijlstra

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue46941>
_______________________________________


More information about the Python-bugs-list mailing list