0

When I am doing coding by the time I found this weird behavior in Python.
Why address or id is different for the string which consists special characters but not for the string which does not have special characters ?

'''

>>> a = 'hai'
>>>
>>> id('hai')
44289760L
>>> id(a)
44289760L
>>>
>>>
>>> del a
>>>
>>> a = 'hai@'
>>> id('hai@')
44289040L
>>>
>>> id(a)
44288840L

'''

Abdulvakaf K
  • 606
  • 1
  • 7
  • 16
  • 3
    what weird behaviour? – Netwave May 03 '19 at 03:58
  • This comes down to micro-optimizations that are implementation details of CPython. Read the linked duplicate if you are intereseted in these details, but note, you should never rely on these behaviors. Given the naive semantics of python, you would expect all these strings to have different id's. – juanpa.arrivillaga May 03 '19 at 07:05

0 Answers0