Assignment in python is exactly that. Assignment, not copying. For those of us who have switched to the language of the gods from some inferior mortal language (Matlab), this can lead to some frustration.

For example, within my Variational Factor Analysis (VBFA) class, I need to keep a record of something I’m calling `b_phi_hat`

. One of the the methods in the class involves the update of this little vector, which depends on its initial (prior) value, `b_phi`

. Like this:

class VBFA: import numpy as np def __init__(self): self.b_phi = np.mat(np.zeros((5,1))) #blah blah... def update_phi(self): self.b_phi_hat = self.b_phi for i in range(5): self.b_phi_hat[i] = self.something()

`update_phi()`

get called 100s of times when the class is used. Spot the problem? It’s on line 8, where `b_phi_hat`

is *assigned* to `b_phi`

. When the loop runs on the next two lines, it’s modifying the original, not just a copy of the original, i.e. after the first iteration line 8 doesn’t ‘refresh’ `b_phi_hat`

, it keeps it at its current value.

What I should have written is:

import numpy as np class VBFA: def __init__(self): self.b_phi = np.mat(np.zeros((5,1))) #blah blah... def update_phi(self): self.b_phi_hat = self.b_phi.copy() for i in range(5): self.b_phi_hat[i] = self.something()

which explicitly makes a copy of the original on line 8, refreshing ` b_phi_hat`

with every iteration.