Regular expression

Fernando Pérez fperez528 at yahoo.com
Mon Nov 26 18:02:47 CET 2001


Sang DeSibtis wrote:


> 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)
> 

No. First, line has to be a string for the above to work. If it's a list, you 
get:

TypeError: expected string or buffer

Now, if line is a 'line' of your open file (meaning it's one string), the 
above re.sub() will make a temporary new string in memory with the new value 
('aaaa'), assign this to line, and the old value of line will now be junk in 
memory. That junk will get discarded when the garbage collector gets around 
to it.

Cheers,

f.



More information about the Python-list mailing list