如何在类中调用函数?

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)
    ...

这篇关于如何在类中调用函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何在类中调用函数?

基础教程推荐