How to pass a function as an argument in Python

Passing function as an argument in Python:

A function can take multiple arguments can be objects, variables(of same or different data types) and functions. Python functions are first class objects. In the example below, a function is assigned to a variable. This assignment doesn’t call the function. It takes the function object referenced by shout and creates a second name pointing to it, yell.

def shout(text):
    return text.upper()
print(shout('Hello'))
yell = shout
print(yell('Hello'))

Output:

HELLO
HELLO

 

Leave a Comment

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

Scroll to Top