如何在类中调用函数? [英] How to call a function within class?

查看:356
本文介绍了如何在类中调用函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have this code which calculates the distance between two coordinates. The two functions are both within the same class.

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

However how do I call the function distToPoint in the function 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)
        ...

推荐答案

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

Since these are member functions, call it as a member function on the instance, self.

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆