Recently I've been learning python and I have been working with Sets and the map function. I noticed some behavior that I don't quite understand. Say I have the code:
1 A = set()
2 l = [1, 2, 3, 4, 5]
3
4 map(A.add, l)
From my understanding of map, all line 4 should do is return an iterator over the function A.add() on each element of l, thus not actually modifying A (Which it doesn't as expected).
However, if I replace line 4 with: set(map(A.add, l)) or list(map(A.add, l)) or even tuple(map(A.add, l)), A is now modified to the set {1, 2, 3, 4, 5}. Why does simply casting the return value of map change what happens to A?
My guess as to why this is happening is that line 4 in the first example is simply creating the iterator whereas when I cast it the iterator is actually iterated through in order to perform the cast, thus actually making the A.add() function calls and populating A with the expected values.
set(),list(), andtuple()are functions that wholly materialize an iterable argument (whether the result of amap()or anything else) to construct a container object of the appropriate type.