First Class functions in Python
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:
- Assign functions to variables.
- Pass them as arguments to other functions.
- Return them from functions.
- Store them in data structures such as lists or dictionaries.
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:
def msg(name):
return f"Hello, {name}!"
# Assigning the function to a variable
f = msg
# Calling the function using the variable
print(f("Emma"))
Output
Hello, Emma!
Explanation:
- The function msg is assigned to the variable f.
- Now f can be used to call msg, showing that functions behave like variables.
2. Passing Functions as Arguments
Functions can be passed as arguments to other functions, enabling higher-order functions.
Example:
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"))
Output
Hello, Alex!
Explanation:
- The function fun1 takes another function (fun2) as input.
- msg is passed to fun1, which then calls it with "Alex".
3. Returning Functions from Other Functions
A function can return another function, allowing for the creation of function factories.
Example:
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:
- The function fun1 defines another function fun2 and returns it.
- func stores the returned function fun2, which can be executed later.
4. Storing Functions in Data Structures
Functions can be stored in data structures like lists or dictionaries.
Example:
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))
Output
8 2
Explanation:
- Functions add and subtract are stored in a dictionary.
- They are accessed using their keys and executed directly.
Which of the following best describes a first-class function in programming languages?
-
A
A function that can only be called from within its own scope.
-
B
A function that can be assigned to variables, passed as arguments, and returned from other functions like any other object.
-
C
A function that can only be defined at the top level of a module.
-
D
A function that can only be executed in a specific context.
In the context of first-class functions, what is a higher-order function?
-
A
A function that can only return primitive data types.
-
B
A function that can take other functions as arguments or return them as results.
-
C
A function that is defined within another function but cannot be returned.
-
D
A function that executes in a separate thread.
What will be the output of the following code?
def greet(name):
return f"Hello, {name}"
say_hello = greet
print(say_hello("Geek"))
-
A
Hello, greet
-
B
greet
-
C
Hello, Geek
-
D
TypeError
Functions can be assigned to variables. Here, say_hello becomes another reference to greet.
Which of the following is not a property of first-class functions?
-
A
Functions can be stored in data structures
-
B
Functions can be assigned to variables
-
C
Functions can return other functions
-
D
Functions can only return primitive types
First-class functions can return any type, including other functions—not limited to primitives.
What will be the output of this code?
def outer():
def inner():
return "Inner function"
return inner
func = outer()
print(func())
-
A
Inner function
-
B
inner
-
C
outer
-
D
Error
outer() returns the inner function, and func() calls it, returning its string.
Which of the following is NOT a characteristic of first-class functions?
-
A
They can be assigned to variables.
-
B
They can be passed as arguments to other functions.
-
C
They can only be defined once in a program.
-
D
They can be returned from other functions.
What is the output of the following code?
def make_multiplier(n):
def multiplier(x):
return x * n
return multiplier
double = make_multiplier(2)
print(double(5))
-
A
7
-
B
10
-
C
25
-
D
Error
make_multiplier(2) returns a function that multiplies its argument by 2. So, double(5) returns 10.
How does Python treat functions with respect to variables?
-
A
Functions are static objects
-
B
Functions are constants and cannot be reassigned
-
C
Functions are first-class citizens and can be stored, passed, and reassigned
-
D
Functions must be declared global to be used
Being first-class citizens means functions can be assigned, passed, and returned like other objects.
