当我运行以下代码时,我得到一个AttributeError:'set'对象没有属性findMean。我做错了什么?
class BasicStats:
def findMean(self, num = {}):
length = len(num)
sum = 0
for x in num:
sum = sum + x
mean =sum/length
return mean
def findVariance(self, num = {}):
mean = self.findMean(num)
length = len(num)
squared_difference = 0
for x in num:
squared_difference = squared_difference + (x-mean)**2
variance = squared_difference/length
return variance
arr = {1, 23, 343.34, 2}
findVariance(arr)
答案 0 :(得分:3)
这是因为该范围内的self
是set
。更具体地说,它是arr
(这是一个集合,你作为第一个参数传入)。
self
关键字仅适用于针对类实例调用的函数(此特殊类型的函数称为方法,请阅读更多here。)