How to call a function within class?

前端 未结 2 773
暗喜
暗喜 2020-11-21 23:37

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 di

相关标签:
2条回答
  • 2020-11-22 00:25

    That doesn't work because distToPoint is inside your class, so you need to prefix it with the classname if you want to refer to it, like this: classname.distToPoint(self, p). You shouldn't do it like that, though. A better way to do it is to refer to the method directly through the class instance (which is the first argument of a class method), like so: self.distToPoint(p).

    0 讨论(0)
  • 2020-11-22 00:34

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

    def isNear(self, p):
        self.distToPoint(p)
        ...
    
    0 讨论(0)
提交回复
热议问题