How can I call a function within a class?(如何在类中调用函数?)
问题描述
我有这个计算两个坐标之间距离的代码。这两个函数都在同一个类中。
但是,如何在函数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)
...
这篇关于如何在类中调用函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!