Please look at the code snippet below, I asked the question below
class SAMPLES:
x = np.zeros(10)
def __init__(self, k, value):
self.x[k] = value
a = SAMPLES(0, 9)
b = SAMPLES(0, 10)
print(a.x[0])
print(b.x[0])
OUTPUT:
10
10
But the output must be:
9
10
How should I solve this problem?
self.x = np.zeros(10)in the__init__method. Using a class attribute shares the same array for all instances.xhas the same underlying data across all instances ofSAMPLES.xhas been declared as a static variable instead of a instance variable. see this