I have a question about the map function in Python.
From what I understand, the function does not mutate the list in which it works, but creates a new one and returns it. Is it correct?
In addition, I have the following code snippet
def reflect(p,dir): if(dir == 'X'): func = lambda (a,b) : (a * -1, b) else: func = lambda (a,b) : (a, b * -1) p = map(func,p) print 'got', p
points is an array of tuples, for example: [(1, 1), (-1, 1), (-1, -1), (1, -1)]
If I call the function above in this way:
print points reflect(points,'X') print points
the points list does not change. Inside the function, however, the print function correctly prints what I want.
Maybe someone can point me in some direction where I could find out how all this is passed by value / reference, etc. works on python and how can I fix this? Or maybe I'm trying too hard to imitate Haskell in python ...
thanks
edit:
Say instead of p = map(func,p) I do
for i in range(len(p)): p[i] = func(p[i])
The list value is updated outside the function, as if working by reference. Fu, hope this is clear: S