Python Strings

Jonadab the Unsightly One jonadab at bright.net
Tue Sep 5 07:16:16 EDT 2000


CHRIS <unk at unk.ororr> wrote:

> Also, what's the point of strings being immutable 

It lets the interpreter (or compiler, as the case may be) 
assume they won't change, and that assumption allows some
optimisations.  Inform strings are immutable too (which
allows them to be stored in ROM, which, if your program
starts approaching "hardware"[1] limitations can be quite 
handy).  But you can always do one of two things:

1.  Construct a new string (out of parts of the old string
    and whatever new characters you want) and assign it
    to the variable that used to hold the old string.  This
    for all practical purposes is the same thing as changing
    the string, and in fact string "altering" functions 
    could be written based on this that would fake the
    change-the-string behavior as long as you're not
    relying on C-like weirdness, such as a separate pointer
    which addresses the same memory space.

2.  If you need to make *frequent* alterations, use an
    array of characters instead.  This is handy for 
    buffers that initially hold user input that you
    want to mung before proceeding (such as with a 
    BeforeParsing routine).  

[1]  Well, virtual hardware.  Inform compiles to two 
     platforms, both virtual.  

- jonadab



More information about the Python-list mailing list