3

till now I belived that Python strings are immutable but when I tried this I got confused

>>> a
'hello world !'
>>> id(a)
140521043795728
>>> a+="d"
>>> id(a)
140521043795728
>>> a+="d"
>>> id(a)
140521043795728
>>> a
'hello world !dd'

if I use += operator, it doesn't change the object id although string has changed. what is happening in this case?

now this is bit different? https://ideone.com/eg1SIN

can somebody explain what is happening here?

bakar
  • 1,152
  • 2
  • 11
  • 20
  • Try adding `b = a` before appending, leaving `b` unchanged, and printing both the id's of `a` and `b`. – Colonel Thirty Two Oct 02 '15 at 16:25
  • @ColonelThirtyTwo: https://ideone.com/eg1SIN what is with this? – bakar Oct 02 '15 at 16:27
  • @William_Wilson: yeah i have seen but thats bit different observation. – bakar Oct 02 '15 at 16:31
  • Python simply reused the ID, but the phenomenon of immutable strings and variables is exactly the same. I do however see the use in expanding the information found in the link I posted. – William_Wilson Oct 02 '15 at 16:34
  • That is the memory address, you no longer use the original `a` object so python is free to reuse the address. As Colonel commented, keep a reference to `a` and you will see different behaviour. – Padraic Cunningham Oct 02 '15 at 16:35

2 Answers2

10

id returns an integer value that is unique to the object you pass it. If a is the only reference to the string 'hello world !', then doing a += "b" may end the lifetime of the string 'hello world !' before the string 'hello world !b' is created, so the ID of the old string is reused (which is perfectly valid behavior).

Adding another reference to the string 'hello world !' (for example, by doing b = a before modifying a) should cause the IDs to diverge.

Colonel Thirty Two
  • 23,953
  • 8
  • 45
  • 85
  • So what is the logic/algo of creating/assigning `id` to python objects? how python interpreter assign/generate id? any source for this? – bakar Oct 02 '15 at 16:36
  • 1
    The cpython interpreter simply returns the address of the object, but that's an implementation detail. The only guarentees are that for as long as an object is live, it has a unique ID that never changes. – Colonel Thirty Two Oct 02 '15 at 16:37
1

The string objects themselves are immutable, however, according to the Python documentation objects with non-overlapping lifetimes may have the same id() value.

Alex
  • 21,273
  • 10
  • 61
  • 73
  • According to the Python documentation, two objects with non-overlapping lifetimes may have the same id() value. I corrected my answer to reflect this. – Alex Oct 02 '15 at 16:34
  • The *object* is a memory address – Padraic Cunningham Oct 02 '15 at 16:36
  • This is correct. As the builtin help puts it: ```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 it's unique for as long as it exists, but memory addresses can be reallocated during a session. – Ben Oct 02 '15 at 18:52