Regular expression

Hans Nowak wurmy at earthlink.net
Tue Nov 27 19:28:19 EST 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 [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? 

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:

  http://w1.132.telia.com/~u13212494/guides/python-objects.htm

--Hans



More information about the Python-list mailing list