Consider:
args = ['-sdfkj']
print args
for arg in args:
print arg.replace("-", '')
arg = arg.replace("-", '')
print args
This yields:
['-sdfkj']
sdfkj
['-sdfkj']
Where I expected it to be ['sdfkj'].
Is arg in the loop a copy?
It behaves as if it is a copy (or perhaps an immutable thingie, but then I expect an error would be thrown…)
Note: I can get the right behavior with a list comprehension. I am curious as to the cause of the above behavior.
Yes, it contains a copy of the reference.
When you reassign
argyou aren’t modifying the original array, nor the string inside it (strings are immutable). You modify only what the local variableargpoints to.