copying a string???

Joe Connellan joec at mill.co.uk
Thu Aug 22 15:53:46 CEST 2002


The string I pass to changeString() represents an 8 bit image for
processing. The images I am processing are very large so it is more
efficient for my function to change the pixel values of the input image
rather than allocating enough memory for an output image to write into. I
need to display before and after images once the processing is done - at
the moment my before image changes with the after.

If I shouldn't be changing the string, do you know of a better way of
going about it? - I do need the image data to be stored as a string - for
PIL and PyOpenGL reasons, and I can't afford to allocate memory for more
than one image at a time.

Joe


Michael Hudson wrote:

> 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