Mark Lewis
Oct 3, 2021

--

This explanation is somewhat flawed. So see so, change lst.append(4) to lst = [1, 2, 3, 4] and see what happens. Both are passed by reference, but you are changing the local reference for the string while calling a mutating method on the list. Strings in Python (and most other languages newer than C++) are immutable so they don't have any mutating methods. I agree though that this is a huge source of bugs and a good reason why more data structures should be immutable.

--

--

Mark Lewis
Mark Lewis

Written by Mark Lewis

Computer Science Professor, Planetary Rings Simulator, Scala Zealot

No responses yet