假设我要创建一个类,应用一个方法并为结果对象设置一个属性。
arr = np.array([1,2,3])
class Transformer:
def __init__(self, array):
self.array = array
def operator(self):
operator = (self.array * 2) + 60
return operator
@staticmethod
def meta(array):
meta = (max(array) + 17)
return meta
def to_operator(self):
op = self.operator()
meta = self.meta(op)
setattr(op, 'meta', meta)# or op.meta = meta
return op
t = Transformer(np.array([1, 2, 3]))
t1 = t.to_operator()
print(t1.meta())
在这里出现以下错误:
AttributeError:“ numpy.ndarray”对象没有属性“ meta”
预期结果:
>>> 83
答案 0 :(得分:0)
运行代码时,出现错误,错误行显示为setattr(op, 'meta', meta)
:
Traceback (most recent call last):
File "/home/ralf/PycharmProjects/scratch_pad/run.py", line 44, in <module>
t1 = t.to_operator()
File "/home/ralf/PycharmProjects/scratch_pad/run.py", line 29, in to_operator
setattr(op, 'meta', meta)
AttributeError: 'numpy.ndarray' object has no attribute 'meta'
在以下相关问题中了解原因: