String processing inside lists
effbot at telia.com
Mon Feb 28 23:22:32 CET 2000
Gary Moore wrote:
> The problem happened when I tried to use the construct:
> for x in actors
> to process the list of actors prior to output.
> When I use the string library to process this list, I want the actual
> strings contained in the list to change. What I found was that no
> matter how I tried to change the strings in the actors list, I was
> My concern is that I must not be understanding some basic concept
> the FOR loop in Python. I reread the material in 'Learning Python', but
> still could not understand why the first solution above did not work.
you don't grok assignment yet (but you will):
in many programming languages, a variable is
a small region of memory in which you can put
things of a given type.
however, in Python, a variable is a name which
can point to any existing object. variables don't
have types (but objects do).
when you assign a new object to a variable, you
just change the name so it points to the new object.
the old object is not affected by this (in fact, it
doesn't even notice, unless the assigment means
that nobody cares about the object anymore, in
which case it's destroyed)
so in your loop, when you created a new object
using replace and assigned it to "x", you modified
the "x" variable to point to the new object.
the old one (owned by the list) wasn't affected.
hope this helps!
More information about the Python-list