Ste*_*ven 204 python class function call
我有这个代码来计算两个坐标之间的距离.这两个函数都在同一个类中.
但是如何在函数distToPoint中调用函数isNear?
class Coordinates:
def distToPoint(self, p):
"""
Use pythagoras to find distance
(a^2 = b^2 + c^2)
"""
...
def isNear(self, p):
distToPoint(self, p)
...
Run Code Online (Sandbox Code Playgroud)
Jef*_*ado 346
由于这些是成员函数,因此将其称为实例上的成员函数self.
def isNear(self, p):
self.distToPoint(p)
...
Run Code Online (Sandbox Code Playgroud)
Ale*_*amo 40
这不起作用,因为distToPoint在你的类中,所以如果你想引用它,你需要在它前面加上它,如下所示:classname.distToPoint(self, p).不过,你不应该这样做.更好的方法是直接通过类实例(类方法的第一个参数)引用该方法,如下所示:self.distToPoint(p).
| 归档时间: |
|
| 查看次数: |
336540 次 |
| 最近记录: |