Python Forum
is and '==' i'm confused - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: is and '==' i'm confused (/thread-34890.html)



is and '==' i'm confused - hshivaraj - Sep-12-2021

Hi all

>>> a = "this is test string"
>>> b = "this is test string"
>>> a == b
True

>>> a is b
False

>>> a = "test"
>>> b = "test"

>>> a is b
True
Why is a is b True in the second time compared to first time. If appears when the string with multiple words causes the it produce. AIs this because the underlying memory management is different?


RE: is and '==' i'm confused - snippsat - Sep-12-2021

== when comparing values and is when comparing identities(which is object's memory address).
Can use id() to see this.
>>> a = "this is test string"
>>> b = "this is test string"
>>> id(a)
2777033042000
>>> id(b)
2777033041840
>>> a is b
False

>>> help(id)
Help on built-in function id in module builtins:

id(obj, /)
    Return the identity of an object.
    
    This is guaranteed to be unique among simultaneously existing objects.
    (CPython uses the object's memory address.)
So there is underlaying stuff going on eg -5 to 256 most used numbers has a optimization to cache these in same place in memory.
>>> c = 50
>>> d = 50
>>> c is d
True
>>> c = 257
>>> d = 257
>>> c is d
False



RE: is and '==' i'm confused - deanhystad - Sep-12-2021

If you wrote the same instructions in a .py and ran it, it would report that a is b as well as well as a == b, Instead of making two identical str objects, Python would make one and assign it to both a and b. I guess this optimization is not available when interpreting code a line at a time.


RE: is and '==' i'm confused - naughtyCat - Sep-13-2021

is is used to determine whether two variable reference objects are the same (same memory address)
== is used to determine whether the value of the reference variable is equal (same value but different memory address)
arr = [1, 2, 3]
a = arr # a is point to the same object with arr (means same memory address)
b = arr[:] # b is assigned the same value with arr

print(a is arr)
print(b is arr)

# Here you will find that arr and a have the same memory address but not b
print(id(arr))
print(id(a))
print(id(b))
Output:
True False 1897473154560 1897473154560 1897476673216



RE: is and '==' i'm confused - Notabene - Sep-15-2021

(Sep-12-2021, 04:50 PM)snippsat Wrote: == when comparing values and is when comparing identities(which is object's memory address).
Can use id() to see this.
>>> a = "this is test string"
>>> b = "this is test string"
>>> id(a)
2777033042000
>>> id(b)
2777033041840
>>> a is b
False

>>> help(id)
Help on built-in function id in module builtins:

id(obj, /)
    Return the identity of an object.
    
    This is guaranteed to be unique among simultaneously existing objects.
    (CPython uses the object's memory address.)
So there is underlaying stuff going on eg -5 to 256 most used numbers has a optimization to cache these in same place in memory.
>>> c = 50
>>> d = 50
>>> c is d
True
>>> c = 257
>>> d = 257
>>> c is d
False

what about "is None"?


RE: is and '==' i'm confused - deanhystad - Sep-15-2021

There is only one None instance (it is a singleton), so "is None" and "== None" will always be the same. I think "is None" and "is not None" reads better than "== None" and "!= None". Using "== None" will get you teased on the playground.


RE: is and '==' i'm confused - snippsat - Sep-15-2021

(Sep-15-2021, 08:05 AM)Notabene Wrote: what about "is None"?
That's the right way comparisons to a singleton like None should always be done with is or is not as mention bye deanhystad,
not the equality operators.
A example re.match return None if no match.
import re

def foo(arg):
    match = re.match(r'Bus', arg)
    if match is None:
        return 'No match'
    return 'Did match'

print(foo('Taxi')) 
Output:
No match