擅长:python、mysql、java
<p>发生这种情况是因为您正在用构造函数中的<code>0</code>覆盖<code>self.length</code>方法。所以当你试图调用<code>s.length()</code>时,它实际上是在试图调用<code>0()</code>,因为你分配了<code>self.length = 0</code>。你知道吗</p>
<p>您可能应该这样做(注意我在每个x和y值前面加上<code>self</code>,这样它就使用了实例的属性值):</p>
<pre><code>class Segment():
def __init__(self, tup1, tup2):
self.x1 = tup1[0]
self.x2 = tup2[0]
self.y1 = tup1[1]
self.y2 = tup2[1]
self.length = math.sqrt((self.y2-self.y1)**(2+(self.x2-self.x1)**2))
self.slope = ((self.y2-self.y1)/(self.x2-self.x1))
</code></pre>
<p>然后只需访问实例属性即可访问<code>length</code>和<code>slope</code>属性:</p>
<pre><code>>>> s = Segment((1,2),(3,4))
>>> s.length
8.0
>>> s.slope
1
</code></pre>
<p>(同样值得注意的是,您的<code>length</code>函数不太正确,但我将把这个修正留给您!)你知道吗</p>