Raymond Hettinger <[email protected]> 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 <[email protected]>
<https://bugs.python.org/issue46941>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com