Regular expression

Sang DeSibtis desibtis at hotmail.com
Tue Nov 27 18:23:34 EST 2001


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 [1]: s='abc'
> In [2]: s.replace('a','A')
> Out[2]= 'Abc'
> In [3]: s
> Out[3]= '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
address? I am really confused by this; isn't the 'line' the name of
the list object holding the contents of the file 'junk' I opened and
my understanding is, list contents are mutable.

line = re.sub('AAAA', 'aaaa', line)

I don't get it, but thanks for your times.

TIA



More information about the Python-list mailing list