Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
is and '==' i'm confused
#1
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?
Reply
#2
== 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
jefsummers likes this post
Reply
#3
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.
Reply
#4
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
Reply
#5
(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"?
Reply
#6
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.
Reply
#7
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pandas confused DPaul 6 312 Sep-19-2021, 06:45 AM
Last Post: DPaul
  Confused with 'flags' tester_V 10 1,370 Apr-12-2021, 03:03 AM
Last Post: tester_V
  Simple Tic Tac Toe but I'm confused Izith 1 716 Sep-26-2020, 04:42 PM
Last Post: Larz60+
  I am really confused with this error. Runar 3 914 Sep-14-2020, 09:27 AM
Last Post: buran
  Confused by 'break' in the official documents Chuck_Norwich 2 884 Apr-12-2020, 09:26 PM
Last Post: Chuck_Norwich
  Confused on how to go about writing this or doing this... pythonforumuser 3 1,034 Feb-10-2020, 09:15 AM
Last Post: snippsat
  I always get 'None' returned. Confused. What did I miss? jpezz 2 1,143 Apr-07-2019, 10:06 AM
Last Post: jpezz
  Confused by order of operations ward 4 1,298 Jan-22-2019, 08:53 PM
Last Post: Larz60+
  Confused by this modulo equation VikramSuh 3 1,529 Sep-05-2018, 12:43 PM
Last Post: VikramSuh
  I am new to python , i am confused with this syntax , please help aankrose 3 1,471 Aug-03-2018, 06:49 PM
Last Post: aankrose

Forum Jump:

User Panel Messages

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