wurmy at earthlink.net
Wed Nov 28 01:28:19 CET 2001
Sang DeSibtis wrote:
> Fernando Pérez <fperez528 at yahoo.com> wrote in message news:<9tvbv4$2uo$1 at peabody.colorado.edu>...
> > > Sang DeSibtis wrote:
> > >>
> > >>
> > >> Questions: how do I make a global replacements in the 'line' that
> > >> read from the file object (buffer in memory? ).
> > The key thing to understand is that in Python, strings are immutable, so you
> > can never do *anything* in place to a string. All string operations return a
> > new string, which if you want to work 'in place' you can just reassign to
> > your original string:
> > string = do_whatever(string)
> > Even string methods don't work in-place:
> > In : s='abc'
> > In : s.replace('a','A')
> > Out= 'Abc'
> > In : s
> > Out= 'abc'
> > Cheers,
> > f
> Thanks! It solves my problem and also leads to another one. Isn't the
> 'line' on both side of the assingment are using the same memory
No no... Python "variables" (they aren't really) do not work in the
same way as variables in more traditional languages like C, Pascal
or Basic. What looks like assignment in other languages, is in
Python the binding of a name to an object:
x = 2
creates an object of type integer, with value 2, and binds it to the
name "x". It does *not* change the value of a typed variable at a
certain memory address.
Read this excellent guide for more information:
More information about the Python-list