5 Most Popular Ways to Call a Function in Python

Creating a function in Python is one thing, but calling it is another. In order to get the most out of a function, be it the one you’ve built yourself or the one that’s built into the programming language, you need to know the most suitable way to call it for the specific purpose you want to utilize it for. In this post, I’ve compiled a list of the 5 most popular ways to call a function in Python which are:

  1. Directly calling the function
  2. Using a function pointer
  3. Using the globals() and locals() dictionaries
  4. Using a lambda function
  5. Using the exec() function

For each one of these, I’ll show you when you should use it, and examples of how to use them. I also have to say that this list is not the complete list of all methods to call a function, but I’m confident that it will cover most of your use cases, especially if you’re a beginner. Let’s get started.

1. Directly calling the function

Direct calling is perhaps the simplest and most straightforward way of calling a function in Python. In order to use this method, you have to write the name of the function followed by a period and the function’s arguments in parentheses.

def greet(name):
    print("Hello, " + name)


In the above example, we create a function called greet() which takes in the argument name and concatenates it with the ‘Hello’ greeting. When we call it, we specify the function name and the argument and the result of the function call is printed on the console as ‘Hello John’.

You can also use variables to store a function. When you call it, you can now use the variable name instead of the original function name; Like below:

def greet(name):
    print("Hello, " + name)

func = greet

Using variables to call functions is useful when you want to reuse the same function in multiple places in your code.

2. Using a Function Pointer

A function in Python can be treated like any other object, including assigning it to a variable or storing it in a list or dictionary. This allows us to pass a function as an argument to another function or store a reference to the function in a variable.

def multiply(x, y):
    return x * y

def raise(x, y):
    return x ** y

def apply_operation(func, x, y):
    return func(x, y)

multiply_result = apply_operation(multiply, 3, 4)
print(mult_result)  # Output: 12

raise_result = apply_operation(raise, 2, 3)
print(raise_result)  # Output: 8

In this example, we have two functions, mult() and raise(), that perform simple arithmetic operations of multiplying and raising a number to a power respectively. The apply_operation() function takes a function func as its first parameter, followed by x and y. Inside the function, x and y are passed to the func() function and the result is returned.

We can pass either the multiply() or raise() function as the func argument to the apply_operation() function and get the result of the respective operation.

The multiply_result and raise_result variables store the results of calling apply_operation() with multiplyand raise as the func argument, respectively.

3. Using the globals() and locals() functions

The globals() and locals() functions can be used to call functions in Python. The globals() function contains all the global variables in the current scope, while the locals() function contains all the local variables in the current scope.

def multiply(x, y):
    return x * y

globals()["multiply"](3, 4)

result = locals()["multiply"](3, 4)
print(result)  # Output: 12

In this example, the multiply() function takes two arguments, x and y, and returns their product, just like from the previous method. To call the function using the globals() function, we simply access the function using its name as a key in the dictionary.

Similarly, to call the function using the locals() dictionary, we access the function using its name as a key in the locals() dictionary and store the result in the result variable, which is then printed to the console.

4. As a Lambda Function

You can create an anonymous function (lambda) and call it at that instance. For example:

greet = lambda name: print("Hello, " + name)

In this example, we define a lambda function using the lambda keyword, followed by its arguments, which in this case is name, then a colon, and the expression that the function should evaluate.

We store the result of the lambda function in the greet variable. The greet() function can then be called like any other function,and the result will be printed in the console as ‘Hello Sarah’.

Lambda functions are useful when you have a small, one-time use function that you don’t want to reuse elsewhere in your code.

5. Using the exec() Function

The exec() function can be used to execute code in Python, including calling a function.

def multiply(x, y):
    return x * y

result = exec('multiply(3, 4)')
print(result)  # Output: 12

In this example, the multiply() function is defined as usual. The exec() function takes a string argument that contains the code to be executed, in this case the call to the multiply() function. The result of the exec() function is stored in the result variable, which is then printed to the console.

And that concludes our guide on ways to call a function in Python.

For all the above methods I’ve mentioned, the appropriate one depends on the requirement and the situation of your use case. Whether you are using the straightforward method of directly calling the function or the more complex method of using the exec() function, the goal is always to make the code as clear, concise, and efficient as possible. Peace!

5 Most Popular Ways to Call a Function in Python

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top