Search⌘ K
AI Features

First-Class Functions

Discover how functions in Python are first-class objects that can be assigned to variables, stored in data structures, passed as arguments, and returned from other functions. Learn about higher-order functions and nested functions to deepen your functional programming skills.

Function: A first-class object

A first-class object is an object that can be:

  • Created at runtime (assigned to a variable)
  • Stored in a data structure
  • Passed as an argument to another function
  • Returned as a value from another function

Several data types in Python are first-class objects, e.g. int, string, etc. But that’s not it. Functions in Python are also first-class objects. Without any further ado, let us run a program demonstrating the nature of functions as first-class objects.

Python 3.10.4
def hello():
return 'Welcome to Educative'
message = hello # Assigning function as an object
print(message())
l = ['hi', message, 'bye'] # Storing function in list
print(l)
# Returning function from another function
def greeting():
return message()
print(greeting()) # Passing function in built-in function

In the above code, in the beginning, we make a function hello that returns a string. Let’s see if this function clears all the requirements for being a first-class object.

  • Look at line 4. We assign hello to a variable message, in the same way, we assign one variable to another. Now, the interesting part is that we can call the function as message(), too. It will return the same string hello() was supposed to return.

  • We store the function in a list l as an object at line 7. It is accessed in the same way; we access the other elements from the list.

  • At line 11, we make another function and that is only returning a function: message().

  • At line 14, we pass the message() function as an argument to a built-in function print().

Note: A function that takes a function as an argument or returns a function is called a higher-order function. In the above example, greeting() and the built-in function print() are both higher-order functions.

Nested functions

A function defined in another function is called a nested function. Nested functions are also referred to as inner functions.

Each time we call an outer function, it defines the inner function.

Run the following example to get a hold on what’s described above.

svg viewer
Python 3.10.4
def outer(x): # Outer function
def inner(y): # Inner function
return y+5
return inner(x)
x = 10
call = outer(x)
print(call)

In the code above, we make a function outer at line 1. It takes an integer x as a parameter. Inside the outer function, we make an inner function: inner at line 3. The inner function takes an integer y and returns y+5.

Now, look at line 8. We declare a variable x and initialize it with a value of 10. Then, we call the outer function which calls the inner function. The call is the final result from the nested function which is 15.

So, the bottom line is that everything in Python is an object, including functions. You can assign them to variables, store them in data structures, and pass or return them to and from other functions.