<p><strong>首先</strong>,了解变量的范围。如果一个变量是在一个方法中声明的,除非它声明为全局变量,否则只能从该方法访问它</p>
<p>当涉及到代码时,请在类中使用variance方法</p>
<pre><code>def variance():
for i in point_list:
var1=sqrt(((i-ave1)**2)/len(point_list))
</code></pre>
<p><code>variance</code>方法将如何理解<code>point_lits</code>变量。它既没有定义也没有声明为全局/类变量</p>
<p><strong>其次</strong>,类的方法将采用通常定义为<code>self</code>的默认参数,除非它被修饰为classmethod<a href="https://stackoverflow.com/questions/2709821/what-is-the-purpose-of-the-word-self">check here</a>了解self关键字</p>
<p><strong>第三个</strong>,类将具有对象,但函数不会。因此,您无法通过函数查看绑定对象</p>
<p>所以,添加self关键字后的代码如下所示</p>
<pre><code>from os import path
class Taraz:
def __init__(self, file_name, file_dir, your_point):
self.file_name = file_name
self.file_dir = file_dir
self.your_point = your_point
self.point_list = None
self.ave1 = None
def sum_ave(self):
f = pandas.read_excel(path.join(self.file_dir, self.file_name))
self.point_list = f['point'].tolist()
sum1 = sum(self.point_list)
self.ave1 = sum1 / len(self.point_list)
def variance(self):
if self.point_list is not None and self.ave1 is not None:
for i in self.point_list:
var1 = sqrt(((i-self.ave1)**2) / len(self.point_list))
def taraz1(self):
taraz1 = (self.your_point - self.ave1) / var1
print(taraz1)
</code></pre>
<p>编辑:</p>
<pre><code>>>> def func():
... pass
...
>>> class cla:
... pass
...
>>> func()
>>>
>>> cla()
<__main__.cla object at 0x0000019A55944550>
>>> func
<function func at 0x0000019A552D2EA0>
>>> cla
<class '__main__.cla'>
</code></pre>
<p><code>()</code>用于调用方法或函数。这里<code>func</code>是一个函数<code>cla</code>是一个类。当您调用一个类时,它将返回该类的对象,因此您可以看到<<strong>main</strong>.cla对象位于0x0000019A5594450>;,但当您调用函数时,它将返回函数的响应。因为我的函数在这里没有任何内容,所以它没有返回任何内容</p>