Python Forum
Comparison Operator "is" idle vs python command
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Comparison Operator "is" idle vs python command
#1
I don't understand why the following results aren't the same. c = (a is b) returns True while (a is b) in python idle returns False. What aren't I understanding?

Pycharm/VS Code

a = "many paths"
b = "many paths"
c = (a is b)
print (c)  # This returns True
PYTHON:
>>> a = "many paths"
>>> b = "many paths"
>>> a is b
False

That is supposed to be a print ( c )
Reply
#2
The is operator checks if two variables point to the same object
== checks to see if two variables contain the same data
if 'is' returns True, == must also be True, but the opposite may or mayy not be True.
Reply
#3
Here's what I get with various length strings, on my machine, using python 3.7.
>>> a = "blah"
>>> b = "blah"
>>> a is b
True
>>> c = "blah blah blah"
>>> d = "blah blah blah"
>>> c is d
False
is checks whether two objects are the same. That's unrelated to whether or not they're equal to each other. The reason this sometimes gives True for strings, is because small strings will be cached and re-used (sometimes), because they're immutable and can't change. Not all python implementations do this, though, which is why you shouldn't be using is to compare strings. You should only be using it to compare with None, anything else should be using equality checks ==.
Reply
#4
Edit: Even the results in REPL are not always reproducibly. Don't use the is operator for str, bytes, int, float.

I guess the AST-Parser and/or Bytecode optimizer scans for equal literals and replaces them with a reference to the same object.
This explains why the first result is True and the second one False.
In interactive mode (repl) a and b referring to two different str objects.

Use only the is operator, if you want to check Identity.
You can check the identity of Singletons like None, True, False, ....
Also of all other objects like list, tuple, dict, set, functions, classes, types.
Surprise, also for str and bytes, but not if you create new literals.
WORK = "work"
RUN = "run"
STOP = "stop"

def some_function(choice):
    if choice is WORK:
        print('Go to work')
    elif choice is RUN:
        print('You are late. Run baby..')
    elif choice is STOP:
        print('Go Home.')
    else:
        print('Got invalid input')

some_function("work") # <- invalid input
some_function(WORK)
"work" is not WORK and "run" is not RUN and "stop" is not STOP
The good thing is, if you enter this in Python 3.8, you get a nice SyntaxWarning and there is some auto correction going on in the background.
Error:
SyntaxWarning: "is not" with a literal. Did you mean "!="?
With mutable objects this should be clear.
d1 = {}
d2 = {}
The name d1 refers to a dict
The name d2 refers to another new dict
Same for tuple- list-literals.

Now a example with immutable objects:
a = 5
b = 5
Name a and name b should refer to two different objects.
But this it not the case. The Python interpreter creates a range of numbers in memory and literals like the 5 is reusing the object for 5 from memory. But this is not the case for bigger numbers. You know, memory is limited.

The differences you see, comes from optimization and implementation details.
If you want to check for equality, don't use the is operator.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why can I not see the code correctly in Python IDLE.? Trump 7 438 Mar-14-2024, 09:05 PM
Last Post: deanhystad
  Launch Python IDLE Shell from terminal Pavel_47 5 1,143 Feb-17-2023, 02:53 PM
Last Post: Pavel_47
Photo String comparison in a csv file in Python Pandas fleafy 2 1,108 Nov-18-2022, 09:38 PM
Last Post: fleafy
  Python Idle won't start totalmachine 9 3,382 Oct-16-2022, 05:57 PM
Last Post: totalmachine
  Why is IDLE not opening on a MacOS Montery 12.6 and using Python 3.10.7? Merlin385 7 1,592 Oct-08-2022, 08:36 PM
Last Post: Merlin385
  MAC Python IDLE issue shadow12 4 2,462 Oct-29-2021, 12:22 AM
Last Post: shadow12
  Python MYSQL connection does not work after 1h idle zazas321 9 6,622 Oct-07-2021, 12:02 PM
Last Post: ndc85430
  Customize Python Keywords for IDLE alanvers 0 1,973 Apr-03-2021, 10:56 AM
Last Post: alanvers
  Python greater than equal to comparison operator Ilangos 4 2,356 Sep-26-2020, 03:53 AM
Last Post: buran
  Python logical operator AND rasec70 4 2,479 May-07-2020, 03:40 PM
Last Post: pyzyx3qwerty

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020