How to call a function inside a class in python

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?

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

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

How to call a function inside a class in python

asked Apr 11, 2011 at 0:20

0

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

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

answered Apr 11, 2011 at 0:24

Jeff MercadoJeff Mercado

124k31 gold badges239 silver badges258 bronze badges

4

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

answered Apr 11, 2011 at 0:24

Aleksi TorhamoAleksi Torhamo

6,2122 gold badges32 silver badges42 bronze badges

6

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Prerequisite: Functions in Python
    In Python, any written function can be called by another function. Note that this could be the most elegant way of breaking a problem into chunks of small problems. In this article, we will learn how can we call a defined function from another function with help of multiple examples. 

    Calling and Called Function ? 
    The Function which calls another Function is called Calling Function and function which is called by another Function is call Called Function.

    How does Function execution work? 
    A stack data structure is used during the execution of the function calls. Whenever a function is invoked then the calling function is pushed into the stack and called function is executed. When the called function completes its execution and returns then the calling function is popped from the stack and executed. Calling Function execution will be completed only when called Function is execution completes.

    In the below figure. The function call is made from the Main function to Function1, Now the state of the Main function is stored in Stack, and execution of the Main function is continued when the Function 1 returns. The Fucntion1 Calls Function2 now the State of the Function1 is stored stack and execution of Function 1 will be continued when Function 2 returns. 

    How to call a function inside a class in python

    Consider the below Example of the function call. The Function SumOfSquares function calls the Function Square which returns the square of the number. 

    Python3

    def Square(X):

        return (X * X)

    def SumofSquares(Array, n):

        Sum = 0

        for i in range(n):

            SquaredValue = Square(Array[i])

            Sum += SquaredValue

        return Sum

    Array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

    n = len(Array)

    Total = SumofSquares(Array, n)

    print("Sum of the Square of List of Numbers:", Total)

    Output : 

    Sum of the Square of List of Numbers: 385 

    Calling Function From another Function within Same class –
    In the below example, the class method Function1 calls method Function2 from the class.

    Python3

    class Main:

        def __init__(self):

            self.String1 ="Hello"

            self.String2 ="World"

        def Function1(self):

            self.Function2()

            print("Function1 : ", self.String2)

            return

        def Function2(self):

            print("Function2 : ", self.String1)

            return

    Object = Main()

    Object.Function1()

    Output : 

    Function2 :  Hello
    Function1 :  World

    Calling parent class Function from Child class Function –
    Consider the below example the child class method invokes the parent class method. The child class inherits the attributes from the parent class.

    Python3

    class Parent:

        def __init__(self):

            self.String1 ="Hello"

            self.String2 ="World"

        def Function2(self):

            print("Function2 : ", self.String1)

            return

    class Child(Parent):

        def Function1(self):

            self.Function2()

            print("Function1 : ", self.String2)

            return  

    Object1 = Parent()

    Object2 = Child()

    Object2.Function1()

    Output : 

    Function2 :  Hello
    Function1 :  World

    How do you call a function within a class in Python?

    How to call an instance method in the same class in Python.
    class c:.
    def f(self):.
    print("abc").
    def g(self):.
    self. f().
    print("def") Function g( ) calls function f( ).
    class_instance = c().
    class_instance. f().

    What do you call a function inside a class?

    Inner functions, also known as nested functions, are functions that you define inside other functions. In Python, this kind of function has direct access to variables and names defined in the enclosing function. Inner functions have many uses, most notably as closure factories and decorator functions.

    How do you call a function inside another function in the same class?

    Answers (1) First, you can use a dot/period to access the method from the class variable. Second, you can simply call the function and pass the class object as an argument. Using MATLAB's Create a Simple Class Example as a basis, I've added the doBoth function below to illustrate both of these options.

    How do you call a method in another method in Python?

    The Function which calls another Function is called Calling Function and function which is called by another Function is call Called Function. How does Function execution work? A stack data structure is used during the execution of the function calls.