Functions as a First Class Objects in Python

Aditya Dhanraj Tiwari
Python in Plain English
3 min readNov 2, 2020

--

Photo by Hitesh Choudhary on Unsplash

In Python, functions are first class citizens. But what does this mean?

The first class objects are program entity which have these five characteristics:

1. Can be created at runtime.
2. Can be assigned to a variable.
3. Can be passed as a argument to a function.
4. Can be return as a result from a function.
5. Can have properties and methods

Let’s see each point one by one through code.

1. Can be created at runtime

add = eval("lambda a,b : a + b")
print(add(10,20)) # logs: 30
print(type(add)) # logs: <class 'function'>

Here add function is created at runtime and you can also see it is instance of a function class.

2. Can be assigned to a variable

getSquare = lambda value: value * value[print(getSquare(value), end=' ') for value in [10,15,20]]# logs: 100 225 400

Here anonymous function, lambda value: value * value is assigned to variable getSqaure, now we can use getSqaure variable as a function in our program.

3. Can be passed as a parameter to a function

import mathdef customFilter(arr,pred):
return [value for value in arr if pred(value)]
arr = [100,23,45,75,225,36]isPerfectSqaure = lambda x: int(math.sqrt(x)) ** 2 == xprint(customFilter(arr,isPerfectSqaure)) #logs: [100, 225, 36]print(customFilter(arr,lambda x: x > 50)) #logs: [100, 75, 225]

We have created function customFilter which take arr(type list) and pred(type function) as arguments. In customFilter we can pass any function which take one argument and return bool value as a result.

For example we are passing isPerfectSquare function to find elements which are perfect square and in second case passing anonymous function to find elements which are grater than 50.

4. Can be return as a result of a function

from time import timedef wrapper(fun):
def inner(*args):
start = time()
result = fun(*args)
end = time()
print(f'Total time taken: {end - start} s')
return result
return inner
getSum = wrapper(sum)print(type(getSum))
# logs: <class 'function'>
print(getSum(list(range(1,2000000))))
# logs
# Total time taken: 0.1580057144165039 s
# 1999999000000

Here we have created wrapper function, which take function as a argument and return function as a result. What this wrapper do is that it logs time taken to execute the function.
Printing type of getSum shows it is instance of a function class.

5. Can have properties and methods

def getCube(num):
return num * num * num
# Printing properties and methods getCube function object have
print(dir(getCube))
#logs: ['__annotations__', '__call__', '__class__',...] Truncated #due to space
# Printing name property of getCube function object
print(getCube.__name__) #logs: getCube
# Calling getCube function using __call__ methodprint(getCube.__call__(10)) #logs: 1000

Having function as a first class citizen enables Python to write code in Functional programming paradigm.

If you like this article, please do give a clap or if you want to give some suggestion to improve this article do leave a feedback in comment.

Happy Coding.

--

--