如何为每个 class 实例创建一个新对象?
How do I create a new object for each class instance?
请看下面的代码片段,我问了下面的问题
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])
输出:
10
10
但输出必须是:
9
10
我该如何解决这个问题?
在 __init__
方法中声明 x
。
class SAMPLES:
def __init__(self, k, value):
self.x = np.zeros(10)
self.x[k] = value
a = SAMPLES(0, 9)
b = SAMPLES(0, 10)
print(a.x[0])
print(b.x[0])
请看下面的代码片段,我问了下面的问题
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])
输出:
10
10
但输出必须是:
9
10
我该如何解决这个问题?
在 __init__
方法中声明 x
。
class SAMPLES:
def __init__(self, k, value):
self.x = np.zeros(10)
self.x[k] = value
a = SAMPLES(0, 9)
b = SAMPLES(0, 10)
print(a.x[0])
print(b.x[0])