How to call a function within class with Python?

Sometimes, we want to call a function within class with Python.

In this article, we’ll look at how to call a function within class with Python.

How to call a function within class with Python?

To call a function within class with Python, we call the function with self before it.

For instance, we write:

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

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

We call the distToPoint instance method within the Coordinates class by calling self.distToPoint.

self is variable storing the current Coordinates class instance.

Conclusion

To call a function within class with Python, we call the function with self before it.