【发布时间】:2019-02-01 07:15:16
【问题描述】:
我不明白为什么这段代码不起作用:
import numpy as np
class Normalizer:
def __init__(self,x):
self.x = x
def mean(self):
return np.sum(self.x)/np.size(self.x)
def mean_zero(self):
return self.x - self.x.mean()
def new_calc(self):
return self.x.mean_zero()
a = np.random.randint(150,200,(5,8))
heights = Normalizer(a)
print(a)
print(heights.mean())
print(heights.mean_zero())
print(heights.mean_zero().mean())
print(heights.new_calc())
它正确地执行了heghts.mean_zero(),但是在def new_calc(self) 方法中它没有执行它。如果有人可以向我解释这一点,那就太好了。谢谢!
【问题讨论】:
-
mean_zero 是类方法。你不能用你的类的变量来调用它。
-
使用
self.mean_zero()而不是self.x.mean_zero()