0

I am trying to understand the working of id() function in Python for strings. If two objects have same string values, then their id is same.

Ex.1:

>>> alphabet1 = 'a'
>>> alphabet2 = 'a'
>>> id(alphabet1)
2046618561072
>>> id(alphabet2)
2046618561072

But, if strings are of multiple lengths, then id becomes different, even though equal strings are assigned to the objects.

Ex2.

>>> str1 = 'i am'
>>> str2 = 'i am'
>>> id(str1)
2046629110128
>>> id(str2)
2046628799536

Can anyone explain the reason/logic behind this or provide a useful link. I tried to refer this, but did not find the answer.

Thanks for any help !

Edit: As pointed out by @Epsi95, this is the case only if the multi-length strings have special symbols (and I also found when there are spaces as well, but except an underscore). So, a plain multi-length string without spaces and special symbols will give you same id.

Ex3.

>>> s1='asdfghjk44_l'
>>> s2='asdfghjk44_l'
>>> id(s1)
2046629110768
>>> id(s2)
2046629110768
asif
  • 171
  • 6
  • 2
    Why do you *expect* them to have the same id's? It should *surprise* you when they *have* the same id's. IOW: " If two objects have same string values, then their id is same." is not a valid inference. `id`'s are unique for *unique objects* . It is a CPython *implementation detail* that single-character strings are interned. That is all. – juanpa.arrivillaga Jan 26 '21 at 15:57
  • "Two objects with non-overlapping lifetimes may have the same id() value." -- note "may" as in one should not expect two variables with the same value to have the same memory address. –  Jan 26 '21 at 15:57
  • It's an implementation detail. If you want to know how it's done in CPython you'll have to look at the source code. – Matthias Jan 26 '21 at 15:57
  • @JustinEzequiel well, these are objects with overlapping lifetimes. – juanpa.arrivillaga Jan 26 '21 at 15:58
  • If you want to compare *equality* don't look ad `id` or at `is`, use `==`. – juanpa.arrivillaga Jan 26 '21 at 16:02
  • @M.Asif it happens for addition of any special character. Ex:`>>> alphabet1 = 'a?' alphabet2 = 'a?'` will have different id – Epsi95 Jan 26 '21 at 16:07
  • 1
    @Epsi95 it's not "special" characters. Another CPython implementation detail, *valid identifiers* are interned, when used as string literals. – juanpa.arrivillaga Jan 26 '21 at 16:10
  • okay thank you for the information – Epsi95 Jan 26 '21 at 16:13
  • All of these are implementation details, of course, that should *never* affect the way you write code, or that should be relied on in any manner – juanpa.arrivillaga Jan 26 '21 at 16:14
  • what is the meaning of `interned` ? – Epsi95 Jan 26 '21 at 16:14
  • 1
    Thanks everyone for your comments. @Epsi95 String Interning in python means: Storing an instantiated string in memory, so any future references to that same string can be directed to refer to it already in existence, instead of taking up new memory [link](https://medium.com/@bdov_/https-medium-com-bdov-python-objects-part-iii-string-interning-625d3c7319de) – asif Jan 26 '21 at 17:28
  • Additional details: https://medium.com/@bdov_/https-medium-com-bdov-python-objects-part-iii-string-interning-625d3c7319de But your code should never care about the IDs unless you have a very specific and good reason for it. – Donnie Jan 26 '21 at 17:57

0 Answers0