Python中的调用函数 [英] Python call function within class

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

问题描述

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



但是如何调用函数 distToPoint code> isNear ?

  def distToPoint(self,p):

使用pythagoras找到距离
(a ^ 2 = b ^ 2 + c ^ 2)

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


self 。

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


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

However how do I call the function distToPoint in the function isNear?

def distToPoint(self, p):
    """
    Use pythagoras to find distance
    (a^2 = b^2 + c^2)
    """
    ...

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

解决方案

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

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

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

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