<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Python's 'is' operator is used to comparing two string slices in ArcGIS API for Python Questions</title>
    <link>https://community.esri.com/t5/arcgis-api-for-python-questions/python-s-is-operator-is-used-to-comparing-two/m-p/1231603#M7998</link>
    <description>&lt;P&gt;Run the same id check several times to see what it returns&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# id
# Return the identity of an object.
# This is guaranteed to be unique among simultaneously existing objects.
#( CPython uses the object's memory address.)
s = 'a string'
id(s)     # 2625510363760
id(s[:3]) # 2625512614384
id(s[:3]) # 2625512614000
id(s[:3]) # 2625512908528
#
id(s[:3]) is id(s[:3])  # False
#
id(id(s[:3]) == id(s[:3]))  # 140711459289192
id(s[:3] == s[:3])          # 140711459289192&lt;/LI-CODE&gt;&lt;P&gt;Now, wrap your head around the last two lines.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 15 Nov 2022 09:06:50 GMT</pubDate>
    <dc:creator>DanPatterson</dc:creator>
    <dc:date>2022-11-15T09:06:50Z</dc:date>
    <item>
      <title>Python's 'is' operator is used to comparing two string slices</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/python-s-is-operator-is-used-to-comparing-two/m-p/1231591#M7996</link>
      <description>&lt;P&gt;I was experimenting with string slicing in Python, and this is the code I came up with:&lt;/P&gt;&lt;LI-CODE lang="c"&gt;s = 'a string'
print(id(s[3:]), id(s[5:]))
print(id(s[3:])==id(s[5:]))
print(s[3:] is s[5:])
print(s[3:] is s[3:])&lt;/LI-CODE&gt;&lt;P&gt;The following are the outcomes of the code:&lt;/P&gt;&lt;LI-CODE lang="c"&gt;4396519216 4396519216
True
False
False&lt;/LI-CODE&gt;&lt;P&gt;This &lt;A href="https://www.scaler.com/topics/python/string-slicing-in-python/" target="_self"&gt;website&lt;/A&gt; provides various instances, but I'm not sure what they mean. My question is how those string slices are stored in memory, why different slices of a string have the same id, and why do they yield 'False' when compared to using the 'is' keyword with the same id.&lt;/P&gt;</description>
      <pubDate>Tue, 15 Nov 2022 08:15:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/python-s-is-operator-is-used-to-comparing-two/m-p/1231591#M7996</guid>
      <dc:creator>MubashshirHasan</dc:creator>
      <dc:date>2022-11-15T08:15:51Z</dc:date>
    </item>
    <item>
      <title>Re: Python's 'is' operator is used to comparing two string slices</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/python-s-is-operator-is-used-to-comparing-two/m-p/1231603#M7998</link>
      <description>&lt;P&gt;Run the same id check several times to see what it returns&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# id
# Return the identity of an object.
# This is guaranteed to be unique among simultaneously existing objects.
#( CPython uses the object's memory address.)
s = 'a string'
id(s)     # 2625510363760
id(s[:3]) # 2625512614384
id(s[:3]) # 2625512614000
id(s[:3]) # 2625512908528
#
id(s[:3]) is id(s[:3])  # False
#
id(id(s[:3]) == id(s[:3]))  # 140711459289192
id(s[:3] == s[:3])          # 140711459289192&lt;/LI-CODE&gt;&lt;P&gt;Now, wrap your head around the last two lines.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 15 Nov 2022 09:06:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/python-s-is-operator-is-used-to-comparing-two/m-p/1231603#M7998</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2022-11-15T09:06:50Z</dc:date>
    </item>
    <item>
      <title>Re: Python's 'is' operator is used to comparing two string slices</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/python-s-is-operator-is-used-to-comparing-two/m-p/1237020#M8084</link>
      <description>&lt;P&gt;I've been an ArcGIS Python programmer for years and I've never really needed the id() function or the is operator to get real work done.&amp;nbsp; In practice, I only really care about equality (==).&amp;nbsp; Since you are just experimenting, I guess that's reason enough, but it's probably not a rabbit hole you need not delve in.&lt;/P&gt;&lt;P&gt;So here's what happening in the code above.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;s = 'a string'
# 1) The print statement is called with a comma, which means each parameter
# will be evaluated, one at a time, from left to right.
# 2) s[3:] produces a new string with an id
# 3) That string is passed to the id function, which returns its memory address
# 4) That memory address goes into a new int object
# 5) The string is no longer needed, so it is freed
# 6) s[5:] produces a new string, reusing free memory
# 7) That string is passed to the id function, which returns its memory address
# &lt;span class="lia-unicode-emoji" title=":smiling_face_with_sunglasses:"&gt;😎&lt;/span&gt; That memory address goes into a new int object
# 9) The string is no longer needed, so it is freed
# Both objects had the same id at the time they were alive, so the
# same id is printed.
print(id(s[3:]), id(s[5:]))

# Similar to above, each parameter is evaluated left to right.
# This time, print only has one parameter, but that parameter
# is the result of an operator.  The operator resolves its operands
# from left to right.
# Follow steps 1 through 9, paying attention to 4) and 8).
# Int objects are equal to each other if they have the same value
# so True is returned to the print statement.
print(id(s[3:])==id(s[5:]))

# 1) The print statement is called with one parameter, as evidenced by
# its lack of commas
# 2) The print statement's only parameter is the result of the is operator
# 3) The is operator evaluates its operands from left to right
# 4) s[3:] produces a new string with an id
# Note that this new string is still needed for the is operator, so it
# is not freed.  It hangs out in memory
# 6) s[5:] produces a new string with an id
# This new string is also needed for the is operator, so it is not freed either
# 7) The is operator now compares the identity of both living strings,
# sees that they aren't equal, and returns False in both instances.
print(s[3:] is s[5:])
print(s[3:] is s[3:])&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Now, to address&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/215600"&gt;@DanPatterson&lt;/a&gt;&amp;nbsp;'s post:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;s = 'a string'
# Since there are no print statements, I'm not sure how he got results
# that are in the comments.  Perhaps, if run in a terminal, or at different
# times, the new strings did not reuse recently freed memory.
id(s)     # 2625510363760
id(s[:3]) # 2625512614384
id(s[:3]) # 2625512614000
id(s[:3]) # 2625512908528

# 1) The is operator evaluates its operands from left to right
# 2) s[:3] creates a new string with an id
# 3) The id function is called, returning a new int object
# 4) The new string is freed, releasing its memory
# 5) s[:3] creates a new string with an id.  This may or may
# not have been created in the same memory space as 1) was
# 6) The id function is called, returning a new int object
# 7) The is operator attempts to establish identity on the
# two ints that were returned, which have not been freed.
# The ints are different objects, so is returns False.
id(s[:3]) is id(s[:3])  # False

# This is actually the easiest to explain.  Dan could have
# typed id(10&amp;gt;7), id("h" in "PYTHON".lower()), or id(5!=7)
# and still gotten the same memory address.
# What he is doing is grabbing the id of the result of the
# _equality_ operator.  Those results are bools.  The values
# True and False are global objects of type bool.  Because
# they are objects, they each have an id.  Comparator operators
# don't create new instances of type bool, but simply 
# use the existing ones.
id(id(s[:3]) == id(s[:3]))  # 140711459289192
id(s[:3] == s[:3])          # 140711459289192&lt;/LI-CODE&gt;</description>
      <pubDate>Thu, 01 Dec 2022 21:25:38 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/python-s-is-operator-is-used-to-comparing-two/m-p/1237020#M8084</guid>
      <dc:creator>RogerDunnGIS</dc:creator>
      <dc:date>2022-12-01T21:25:38Z</dc:date>
    </item>
    <item>
      <title>Re: Python's 'is' operator is used to comparing two string slices</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/python-s-is-operator-is-used-to-comparing-two/m-p/1237028#M8085</link>
      <description>&lt;P&gt;like I said, with the self-learning explained&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":thinking_face:"&gt;🤔&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 01 Dec 2022 21:36:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/python-s-is-operator-is-used-to-comparing-two/m-p/1237028#M8085</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2022-12-01T21:36:21Z</dc:date>
    </item>
    <item>
      <title>Re: Python's 'is' operator is used to comparing two string slices</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/python-s-is-operator-is-used-to-comparing-two/m-p/1237031#M8087</link>
      <description>&lt;P&gt;Here is another exercise:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# Here we create a tuple containing two new strings as the result
# of slice operations.  Since both strings stick around in memory,
# neither are freed, and their id's will be different.
orange_slices = (s[:3], s[:5])
id(orange_slices[0])
1448227048496
id(orange_slices[1])
1448234245616&lt;/LI-CODE&gt;</description>
      <pubDate>Thu, 01 Dec 2022 21:50:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/python-s-is-operator-is-used-to-comparing-two/m-p/1237031#M8087</guid>
      <dc:creator>RogerDunnGIS</dc:creator>
      <dc:date>2022-12-01T21:50:03Z</dc:date>
    </item>
  </channel>
</rss>

