Python Strings

Alex Martelli alex at magenta.com
Mon Aug 21 10:01:47 EDT 2000


"CHRIS" <unk at unk.ororr> wrote in message news:399F7547.734A249E at unk.ororr...
> How can you make a character-by-character copy of one string to a target
> string, with out using a temporary list type variable? IE (with out

    mycopy="thetarget"

does exactly this -- as far as externally observable effects are
concerned.  In practice, of course, it just binds variable 'mycopy'
to indicate the SAME storage as 'thetarget' already occupied, but,
hey, why not -- since strings _are_ immutable in Python, why take
up twice as much storage to hold two identical, both-immutable
copies...


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

Makes them very handy as dictionary-keys, but of course that is
not the entire answer; some language-designers just like the clean
semantics that are obtained by declaring elementary types to be
immutable -- cfr. Java: it, too, has immutable strings.

> If you want a
> particular variable to be immutable,

That has nothing whatsoever to do with whether the *object* (that
'the particular variable' is bound to at a given time) is mutable
or not.  In Python, just about everything interesting has to do
with *objects*, NOT with 'variables', which are just transient
labels ('post-it' might be a better metaphor) which may happen to
be (rather-feebly) bound to different objects at different times.

> why not have some sort of constant
> option? Like in C:
>
> static const char str[] = "Constant immutable string";

You're thinking of 'variables' as reasonably-persistent names
for *locations in storage* -- a la C.  Not a good mental model
for Python's variables...


Alex






More information about the Python-list mailing list