python - 如何在类中调用函数?

我有这段代码可以计算两个坐标之间的距离。这两个函数都在同一个类中。

但是,如何在函数isNear中调用函数distToPoint呢?

class Coordinates:
    def distToPoint(self, p):
        """
        Use pythagoras to find distance
        (a^2 = b^2 + c^2)
        """
        ...

    def isNear(self, p):
        distToPoint(self, p)
        ...

最佳答案

由于这些是成员函数,因此将其作为实例上的成员函数调用,self

def isNear(self, p):
    self.distToPoint(p)
    ...

https://stackoverflow.com/questions/5615648/

相关文章:

python - 如何在 Python3 中将 'binary string' 转换为普通字符串?

linux - 如何从我的应用程序目录中删除所有 .svn 目录

linux - 在 Bash 的文件路径参数中获取最后一个目录名/文件名

python - 如何在 Python 中分析内存使用情况?

linux - 如何在bash中将所有子目录中的所有文件gzip到一个压缩文件中

linux - 有效地测试一个端口是否在 Linux 上打开?

python - 使用 matplotlib 在单个图表上绘制两个直方图

python - 在 pandas DataFrame 中查找列的值最大的行

linux - 从 grep 中排除 .svn 目录

python - 出错时自动启动 python 调试器