First Class functions in Python (original) (raw)

Last Updated : 12 Mar, 2026

In Python, functions are treated as first-class objects. This means they can be used just like numbers, strings, or any other variable. You can:

This ability allows you to write reusable, modular and powerful code.

Characteristics of First-Class Functions

Functions in Python have the following important characteristics. Let’s see them one by one with examples:

1. Assigning Functions to Variables

We can assign a function to a variable and use the variable to call the function. **Example:

Python `

def msg(name): return f"Hello, {name}!"

Assigning the function to a variable

f = msg

Calling the function using the variable

print(f("Emma"))

`

**Explanation:

2. Passing Functions as Arguments

Functions can be passed as arguments to other functions, enabling higher-order functions.

Python `

def msg(name): return f"Hello, {name}!"

def fun1(fun2, name): return fun2(name)

Passing the msg function as an argument

print(fun1(msg, "Alex"))

`

**Explanation:

3. Returning Functions from Other Functions

A function can return another function, allowing for the creation of function factories.

Python `

def fun1(msg): def fun2(): return f"Message: {msg}" return fun2

Getting the inner function

func = fun1("Hello, World!") print(func())

`

Output

Message: Hello, World!

**Explanation:

4. Storing Functions in Data Structures

Functions can be stored in data structures like lists or dictionaries.

Python `

def add(x, y): return x + y

def subtract(x, y): return x - y

Storing functions in a dictionary

d = { "add": add, "subtract": subtract }

Calling functions from the dictionary

print(d["add"](5, 3))
print(d["subtract"](5, 3))

`

**Explanation: