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
  String int confused janeik 7 1,017 Aug-02-2023, 01:26 AM
Last Post: deanhystad
  I am confused with the key and value thing james1019 3 912 Feb-22-2023, 10:43 PM
Last Post: deanhystad
  Pandas confused DPaul 6 2,467 Sep-19-2021, 06:45 AM
Last Post: DPaul
  Confused with 'flags' tester_V 10 4,787 Apr-12-2021, 03:03 AM
Last Post: tester_V
  Simple Tic Tac Toe but I'm confused Izith 1 2,154 Sep-26-2020, 04:42 PM
Last Post: Larz60+
  I am really confused with this error. Runar 3 2,927 Sep-14-2020, 09:27 AM
Last Post: buran
  Confused on how to go about writing this or doing this... pythonforumuser 3 2,421 Feb-10-2020, 09:15 AM
Last Post: snippsat
  Dazed and confused... RodNintendeaux 10 7,388 May-28-2017, 01:32 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

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