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

标签 python class function call

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

但是,如何在函数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)
    ...

关于python - 如何在类中调用函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5615648/

相关文章:

python - 用于 SVG 路径计算的库

python - 凯拉斯属性错误: 'list' object has no attribute 'ndim'

c# - C# 中的复杂类层次结构

java - 在私有(private)方法中从另一个类访问对象

c++ - 解析有符号和无符号 int 之间的 char 是否未指定?

javascript - 在 JavaScript 中向函数内的对象添加属性?

python - 是否有用于在另一条路径中查找一条路径的补充的 pathlib 方法?

python - 在 C++ 中嵌入 Cython 类方法

c++ - 人们对 C++ 中结构和类的区别最有趣的错误观点是什么?

c - C 中的函数处理数组