Message350966
Further to Karthikeyan Singaravelan comment, the behaviour you see is absolutely correct. The operator isn't behaving differently, it is reporting precisely the truth.
The ``is`` operator tests for object identity, not equality. Python makes no promises about object identity of literals. If you use an immutable literal in two places:
a = 1234
b = 1234
the interpreter is free to use the same object for both a and b, or different objects. The only promise made is that ``a == b``.
The Python interpreter currently caches some small integers for re-use, but that's not a language guarantee, and is subject to change without warning. It has changed in the past, and could change again in the future.
The bottom line is that you shouldn't use ``is`` except to test for object identity, e.g. ``if obj is None``. |
|
Date |
User |
Action |
Args |
2019-09-02 00:38:23 | steven.daprano | set | recipients:
+ steven.daprano, xtreak, Digin Antony |
2019-09-02 00:38:23 | steven.daprano | set | messageid: <1567384703.48.0.593158031129.issue38001@roundup.psfhosted.org> |
2019-09-02 00:38:23 | steven.daprano | link | issue38001 messages |
2019-09-02 00:38:23 | steven.daprano | create | |
|