ok, noob question
I had this code
l开发者_如何学Cist = ['random', 'set', 'of', 'strings']
for item in list:
item.replace('a','b')
print list
the output doesn't show the replacement unless I change the code to this
list = ['random', 'set', 'of', 'strings']
for item in list:
item = item.replace('a','b')
print list
so my question is what exactly is going on? why doesn't the first example automatically assign?
The method is simply returning the new value, it does not modify the existing one.
There is no set rules that specify that all or no methods will do this, you will have to check the documentation for each one to know specifically what it does and what to expect.
Lots of functions are like that.
Take this:
a = 1
b = 2
c = Add(a, b)
would you expect a or b to have new values at this point? This is the same exact thing.
Now, having said that, there is another problem with your code. Under no circumstances will that list actually change.
Why? Because you're not updating the list, you're updating item.
And item is a "copy" of the value in the list. This is not strictly correct, but it suffices for this answer (learn about references for more information.)
In other words, if you change your two examples to this:
list = ['random', 'set', 'of', 'string']
for item in list:
item.replace('a','b')
print item
vs.
list = ['random', 'set', 'of', 'string']
for item in list:
item = item.replace('a','b')
print item
then you will see a difference.
Answer to follow-up question in comment: If you do not store the result anywhere, it is simply lost. Think of it this way: You call up your friend and ask what time it is on his watch. He says "10:45", and you respond with "actually I don't care click."
The second is also wrong
list = [item.replace('a','b') for item in list]
list = ['random', 'set', 'of', 'strings']
for item in list:
item.replace('a','b')
print list
'random' is a string object, 'set' is a string object, etc
list is a collection of string objects
for item in list:
assigns the name (aka identifier) item successively to each of the objects listed by object list. Assigning means that a binding between the name and the object is created.
For each turn of the loop, after the assignement of item to the current object of list, item.replace('a','b')
is executed:
replace() creates a new object having the same value as the object named item but with 'a' replaced with 'b'.
Where does this creation occur ? In the memory. The new object is physically created somewhere in the RAM, an object has an adress, a type and a value.
And then ? Well, and then, nothing. The object is there, in the RAM, and it doesnt get any assignement of a name. So it will die more or less rapidly, that is to say that the bits occupied by the new object will be reused more or less rapidly, without any care of the interpreter because it has no possibility to know that there is an object there, since this object isn't referenced by an identifier.
.
list = ['random', 'set', 'of', 'strings']
for item in list:
item = item.replace('a','b')
print list
It happens roughly the same.
The difference is that after being created, the object resulting from the instruction item.replace('a','b')
is assigned to the name item. Or if you prefer, the name item is re-assigned to the new object.
So, the new object won't die ? Alas, it will. Because at the next turn of the loop, the instruction for item in list
performs a new assignement of name item to the next element of list
.
Now you should also understand why it is a bad practice to call a list with the name list. Because list is the name of a built-in function , and defining list = ['random', 'set', 'of', 'strings']
has a consequence: the original built-in binding between the name list and the object function list() is broken and replaced with a binding between list and the above collection of strings.
精彩评论