Why can I use the same name for iterator and sequence in a Python for loop?
问题 This is more of a conceptual question. I recently saw a piece of code in Python (it worked in 2.7, and it might also have been run in 2.5 as well) in which a for loop used the same name for both the list that was being iterated over and the item in the list, which strikes me as both bad practice and something that should not work at all. For example: x = [1,2,3,4,5] for x in x: print x print x Yields: 1 2 3 4 5 5 Now, it makes sense to me that the last value printed would be the last value