小编典典

类中的Python调用函数

python

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

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

阅读 725

收藏
2020-02-14

共1个答案

小编典典

由于这些是成员函数,因此在实例上将其称为成员函数self。

def isNear(self, p):
    self.distToPoint(p)
    ...
2020-02-14