Beginner: How to copy a string?

dbrown2 at yahoo.com dbrown2 at yahoo.com
Thu Apr 3 21:22:28 EST 2003


Thanks everyone for setting me straight on some important points about
how strings work.  I think I have an improved understanding of the
Python view of things.

I did not need to make a copy of a string in my original case.  My
blunder was in not understanding that for

>>>a='abc'
>>>b=a
and then
>>>a='def' 

the final line does not change b's assignment.  I had somehow thought
I would loose the object 'abc' because b would then refer to 'def'.

But since everyone asked the same question "Horrors! Why in the world
would you ever need to copy a string?" I took it as a challenge.  I
started thinking about some not so esoteric possibilities,
specifically cases where Python deals with the outside world of real
hardware.  So, just for fun, here's a situation where copy might make
sense:

If I have a library that returns data in a "buffer", the library might
return a fixed-length buffer and Python would just assign the object
to an identifier.  Typical cases might be for talking to serial ports
or hardware registers.  If the library is not written in Python (say
it's compiled C or some other external code) then it seems the library
could return that same pointer to the buffer or register every time,
except it might be filled with different data. Perhaps this is not a
correct assumption.  In any case, if I got a string buffer back from
this kind of library I would want to copy the data to another Python
string so I know it won't change later.

Can this situation come about or am I also not understanding function
return values or how external libraries can work?  It seems there are
cases where Python could get a "string" that is not immutable because
the low-level library passes along a string that does not have the
same restrictions as the Python string.

David


> Before anything else ...
> 
> Why do you want to copy a string? What use do you *actually* have for a 
> copy of a string?
> 
> Now, onto the answer.
> 
> 1. In general, you don't. Strings are immutable, so a copy of a string 
> is indistinguishable from the original except by its ID.
> 
> 2. It is impossible to guarantee that you will get a copy of a string. 
> An implementation is free to do whatever it likes with strings e.g. it 
> could decide that *every* string with the same value will refer to the 
> same object - or it could have every string be a separate object.
> 
> 3. CPython interns all literal strings that look like python 
> identifiers. 'abc' (without the quotes) would be a valid python 
> identifier so it is interned. This is done partially so that identifier 
> lookups in dictionaries are as fast as possible.
> 
> 4. A string copy using copy() or [:] is special-cased by the string 
> object to simply return `self`.
> 
> 5. Given all that, to do what you want, the following will work with 
> current versions of Python (no guarantees of future versions though):
> 
>     s = 'abc'
>     print id(s)
>     t = s[:]
>     print id(t)
>     u = t[:1] + t[1:]
>     print id(u)
>     print repr(s)
>     print repr(t)
>     print repr(u)
> 
> Note however what happens if you try to copy an empty string ...
> 
>     s = ''
>     print id(s)
>     t = s[:]
>     print id(t)
>     u = t[:1] + t[1:]
>     print id(u)
>     print repr(s)
>     print repr(t)
>     print repr(u)
> 
> So, I come back to my original question - why do you want to copy a 
> string?
> 
> Tim Delaney




More information about the Python-list mailing list