3 ms·
> Python 3 gets so much of this right. It's one of the things I really loved about python 3 as it allows for correct string handling in most cases (see below).
by exDM69 13y ago
> Python 3 gets so much of this right. It's one of the things I really loved about python 3 as it allows for correct string handling in most cases (see below).
One of the biggest things that I feel Python gets right with the string type is that strings are immutable. It makes a lot of things easier.
It really makes sense to have a good string type for small strings, stored in unicode. Immutability makes everything simpler.
The string type is not a good fit for handling large amounts of text. There are trade offs for efficiency that have to be made to create a handy string type. It really makes sense to have a separate "bytes" type or some kind of StringBuffer for doing big text operations.
- wsc981 13y agoIsn't the string type immutable in many (most?) other languages as well? In Objective-C the default is an immutable string (though optionally one can create mutable strings as well). Lua also uses immutable strings. In Java and C# I think the situation is the same, since if you want to use high performance string manipulation, you'll generally resort some form of StringBuilder helper class.
- meepmorp 13y agoI believe strings are mutable in Ruby.
- reflectiv 13y agoThey are... s = "hello" s << " world" s # hello world
- dbaupp 13y agoIs that allocating a new buffer, leaving the "hello" string to be collected by the GC?
- tedunangst 13y agoNo, it expands the existing buffer. (leaving " world" to be collected). Note that the following is different and more like what you're thinking. a = a + " world"
- jeorgun 13y agoNo, it's operating in place: def append_world(str) str << " world" end a = "hello" append_world(a) a #=> "hello world"
- SideburnsOfDoom 13y agoCorrect, C# and .Net have an immutable string class and a mutable StringBuilder helper class.