I like the Python syntax a lot, but as I’m coming from C++ I don’t get one thing about iterators in Python. In C++ there are 2 kinds of iterators – constant and modifying (non-const). In python it seems (for what I’ve seen) like there is only the first kind and if you want to modify the elements, you have to use the indexing, which doesn’t feel comfortable and so generic to me.
Let me illustrate with a simple example:
ab = ["a", "b"]
for (index, lett) in enumerate(ab):
print "Firstly, lett is ab[index]?", lett is ab[index]
lett = str(index)
print lett
print ab[index]
print "But after, lett is ab[index]?", lett is ab[index]
So I wasn’t able to modify the list with the iterator.
It just makes Lazy copies (see Wikipedia) as I discovered with the is operator, so is there a way to say I want it to be a directly modifying iterator instead using the neat
for variable in iterable_object:
syntax?
prints