I have a pretty simple python-3 code that is puzzling me.
test.py :
import numpy as np
class PARTICLE:
def __init__(self, PosV = np.zeros(3), Mass=0):
self.posV = PosV
self.mass = Mass
def main():
pL = []
for i in range(10):
p = PARTICLE(Mass=0)
pL.append(p)
pL[0].posV[0] = 10 ### ERROR, modifies pL[1].posV[0] as well
pL[0].mass = 42
print(pL[0].posV[0])
print(pL[1].posV[0]) ### Unexpected to be = 10, must be same memory
print(pL[2].posV[0]) ### Unexpected to be = 10, must be same memory
print(pL[0].mass)
print(pL[1].mass)
print(pL[2].mass)
if __name__ == "__main__":
main()
When I run it :
$ python test.py
10.0
10.0
10.0
42
0
0
It seems that when I create a new PARTICLE object, it looks like the default posV for each new particle points to the same block of memory because if I change pL[0].posV[0] it ALSO changes pL[1].posV[1]. However for args that default to scalars (e.g. Mass), changine pL[0].mass does NOT propagate to pL[1].mass.
QUESTION :
- Please explain why modifying
pL[0].posV[0]ALSO changespL[1].posV[0]. What is going on here?
I'm suspect that it has to do with pointers and deep vs shallow copy, but I'm not sure what is exactly what is going on. Intuitively, I'd expect creating a new PARTICLE instance should create a completely new memory instance, with each new PARTICLE object being independent of the previous ones. Clearly that is not the case.