RE: Mutable Bytes

"Mark Hahn" <[email protected]> Sat, 24 Jul 2004 15:54:36 -0700
Newsgroups gmane.comp.lang.prothon.user
Message-ID <000c01c471d1$31c1e230$0d01a8c0@MarkVaio>
> Martin Christensen <[email protected]> wrote:
> 
> Howdy!
> 
> I've been implementing a bitmap index in Python, where I'm 
> using regular strings to store the bitmap. Considering all 
> the time that's spent in ord() and chr(), I'm sure you can 
> imagine that I'll be quite glad of Prothon's Bytes when it 
> comes to porting it. :-)
> 
> However, one thing irks me: when Bytes is meant for raw data 
> manipulation, why is it immutable? I don't imagine it being 
> an overly likely candidate for dictionary keys and such, 
> though of course you never know. I just find it rather 
> surprising that with a data type like this, I still have to 
> resort to something as inefficient as
> 
> data = data[:i] + newbyte + data[i+1:]
> 
> just to change a single byte. Or did I miss something? So my 
> request is to make the Bytes type mutable unless there's some 
> other good reason not to do so.

I'm not up-to-date on all the arguments pro and con on immutability for text
strings, but I'm not sure I see any difference between text strings and
binary strings.  Any argument you make for Bytes could be made for a String.
Correct me if I'm wrong.

If you are worried about inefficiency as opposed to inconvenience, then
there is going to be a fix for this in my next rewrite of the interpreter
later this year.  When you write x = y + z the intrepreter will create a new
object x that is actually just a link to y and z without actually copying
the text from y and z.  After a lot of such manipulation you will end up
with a mess-o-links that only the computer could follow.  Only when you need
the final result will it follow the links and put the final text back
together.  This will be extremely efficient as the text will only be copied
once at the end instead of over and over again.