0

I was trying some recursive code in which I create a string outside the first call of my recursive function, and send that string as an argument to said first call. This string gets passed around a ton of times inside a recursive algorithm.

Inside this function, the string is mutated using +=. This function obviously gets called a lot as its recursive. I assumed each modification to the string wouldn't inadvertently affect the other function as += should create a new instance of the string, but it seems that sometimes it does mutate it and affects other calls of my function.

enter image description here

I did some digging around (picture above) and found that when doing += to a function sometimes it keeps its id, though I don't know if this confirms my suspicion.

Anyone have any idea of what could be happening?

Hamster
  • 133
  • 3
  • 12

1 Answers1

2

This is likely a memory management optimization that keeps the same ID when you are extending the only reference to a string where available memory exists at the end of the string. At some point the preallocated memory block get filled and a new ID needs to be assigned.

To confirm this, if you store a copy of a in a list, you'll see the id change at every iteration (because there is now more than one reference to the string):

a = ""
b = []
for i in range(30):
    a += "A"
    b.append(a)
    print(id(a))
Alain T.
  • 40,517
  • 4
  • 31
  • 51
  • If you make the loop bigger and count how many strings use the same ID, you will see that python is just allocating in 16-byte chunks, which is common. – Mark Nov 16 '21 at 03:39
  • It's changing every 8 allocations on my 64bit MacOS laptop. (could be different depending on the OS) – Alain T. Nov 16 '21 at 03:40
  • That's interesting. Also on a mac and seeing 16-byte chunks. Safe to say it's implementation dependent :). – Mark Nov 16 '21 at 03:41
  • 1
    I'm on Pyhton 3.7. that could explain it as well – Alain T. Nov 16 '21 at 03:42