Apr-17-2020, 07:20 PM
The first time I ran across immutable strings I found it very confusing, but that was coming from a world were there was no garbage collection unless I did it myself. In C you create/protect/cherish your data structures because they are the foundation of your program. In Python they are tossed away like a used tissue.
But I have to say there are some nice things about immutable strings. I don't have to worry about a string changing value in python. In C, if I kept a string I usually made a copy to protect against the string changing outside the scope of my module. Python does that automatically, and only if I need a copy. If the string I am passed never changes it can be used all over the place and still be the only string. No copies unless needed and then the copies are made automatically. How cool is that!
To a lesser extent I like tuples for the same reason. If I put something in a tuple I don't have to worry about it changing. If I want something that acts more like a C array (a shared resource that can be modified and all users have access to the same modified value) I just use a list (or a python array).
But I have to say there are some nice things about immutable strings. I don't have to worry about a string changing value in python. In C, if I kept a string I usually made a copy to protect against the string changing outside the scope of my module. Python does that automatically, and only if I need a copy. If the string I am passed never changes it can be used all over the place and still be the only string. No copies unless needed and then the copies are made automatically. How cool is that!
To a lesser extent I like tuples for the same reason. If I put something in a tuple I don't have to worry about it changing. If I want something that acts more like a C array (a shared resource that can be modified and all users have access to the same modified value) I just use a list (or a python array).