copying a string???

Michael Hudson mwh at python.net
Thu Aug 22 15:20:58 CEST 2002


Joe Connellan <joec at mill.co.uk> writes:

> I have a function that alters the value of a string (written in
> C). (is that bad?)

Yes.

> in the following code
> 
> str1 = "hi there"
> str2 = copy.deepcopy(str1)
> changeString(str2)
> 
> changeString() changes both str2 and str1, where I only want it to
> change str2 - hence the deepcopy().
> 
> I have also tried these
> str2 = copy.copy(str1)
> str2 = str1
> 
> does deepcopy just assume that becuase a string doesn't support
> assignment it can reference rather than copy it?

Note that "string doesn't support assignment" sounds confused.

It doesn't support item or slice assignment.

But, to answer your question, yes.

> anyone know how I can really copy the string?

Not off the top of my head.

> or am I missing something above?

Yes: strings are immutable.  Lots of things rely on this.  Expect
arbitrary breakage as a result of mutating them.

Cheers,
M.

-- 
  Good? Bad? Strap him into the IETF-approved witch-dunking
  apparatus immediately!                        -- NTK now, 21/07/2000



More information about the Python-list mailing list