classification
Title: platform.system() comparison problem
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.7, Python 3.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: steven.daprano, xtreak, Светломир Балевски
Priority: normal Keywords:

Created on 2019-10-03 11:44 by Светломир Балевски, last changed 2019-10-03 12:51 by steven.daprano. This issue is now closed.

Messages (3)
msg353840 - (view) Author: Светломир Балевски (Светломир Балевски) Date: 2019-10-03 11:44
Hi, I am using platform.system(). On linux I am using python3.6. 
>>> platform.system()
'Linux'
>>> platform.system() is 'Linux'
False 
>>> platform.system() == 'Linux'
True 

On Windows with python 3.7:
[‎10/‎3/‎2019 2:42 PM]  
>>> platform.system()
'Windows'
>>> platform.system() is 'Windows'
True
>>> platform.system() == 'Windows'
True 

Is this a known problem?
msg353843 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2019-10-03 12:44
Using "is" could be dependent on how the string is interned to refer to the same string during comparison for identity. Using == is more reliable and correct.
msg353844 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2019-10-03 12:51
This is nothing to do with platform.system. You can see the same behaviour with any string comparison, or ints.

The `is` operator is not a cute way of spelling `==`, it tests for object identity, not equality. It tests whether the two arguments are the same object. The Python interpreter caches some strings, and some ints, to save memory, so the result of ``expression is "somestring"`` will depend on the operating system, the version of Python, what happens to be in the cache, and whatever memory optimization tricks are being used by the interpreter.

Never use ``is`` for testing whether two things are equal. Only use it for testing object identity, like ``obj is None``.

By the way, this question about ``is`` has been asked many, many times on places like Stackoverflow. If you have any further questions about this, you should check the answers there.
History
Date User Action Args
2019-10-03 12:51:19steven.dapranosetstatus: open -> closed

nosy: + steven.daprano
messages: + msg353844

resolution: not a bug
stage: resolved
2019-10-03 12:44:09xtreaksetnosy: + xtreak
messages: + msg353843
2019-10-03 11:44:52Светломир Балевскиcreate