Functions as Objects in Python

Chaitanya Baweja
Dec 24, 2018 · 3 min read

One of the most powerful features of Python is that everything is an object, including functions. Functions in Python are first-class objects.

Image for post
Image for post

This broadly means, that functions in Python:

  • have types
  • can be sent as arguments to another function
  • can be used in expression
  • can become part of various data structures like dictionaries

Sending functions as arguments coupled with the ability to store them in lists can be extremely helpful. I know all of this can be hard to digest so let’s take this step by step.

Functions can be passed as arguments

Let’s see how we can call this function:

L = [1, -2, -5, 6.2]print apply(L, abs)  # [1, 2, 5, 6.2]
# abs is applied on elements passed in L
print apply(L, int) # [1, -2, -5, 6]

We can also pass a self-defined function. For example:

# Function to return square of a number
def sqr(num):
return num**2
print apply(L,sqr) # [1, 4, 25, 38.440000000000005]

The ability to perform the same operation on a list of elements is also provided by a higher-order python function called map.

In its simplest form, it takes in a ‘unary’ function and a collection of suitable arguments and returns an ‘iterable’ which we must walk down.

So the above apply function could become:

for i in map(abs,[1, -2, -5, 6.2]):
print(i)
# Output:
1
2
5
6.2

To read more about the map operation, head over to: Python map

Functions as elements of a list

Now, to call this function:

function_list = [abs,exp,int]print apply_func(function_list,-2.3)  # [2.3, 0.10025884, -2]
# Functions in function_list are applied to -2.3
print apply_func(function_list,-4.6) # [4.6, 0.01005184, -4]

Side Note: In both the above examples, we could have used list comprehensions. List comprehensions in python provide an elegant way of creating lists based on another list or iterator. For example, lines 14-16 in apply_func could become:

output = [f(x) for f in L]

Functions can be assigned to other variables

i = abs
print(i(-2)) # 2

This is because functions operate exactly like objects and can be passed around. But, an idea that is even more powerful is that all objects can behave like functions as well.

Objects as functions

This can be done by defining a __call__ function inside the object. Let’s define a class that when called, prints whatever it stores.

s1 = Printer('Hello') # Defining object of class Printer
# Calling object s1
s1() # Hello

Thus, under the hood ‘calling’ an object means executing the object’s __call__ method.

Key Takeaways

  • The ability to perform the same operation on a list of elements is provided by a higher-order python function called map.
  • Objects can be made callable by defining a __call__ function.

Python Pandemonium

A place to read and write about all things Python.

Chaitanya Baweja

Written by

Machine Learning Engineer | Python | Data Analytics | Economics | Physics

Python Pandemonium

A place to read and write about all things Python. If you want to become a writer for this publication then let me know.

Chaitanya Baweja

Written by

Machine Learning Engineer | Python | Data Analytics | Economics | Physics

Python Pandemonium

A place to read and write about all things Python. If you want to become a writer for this publication then let me know.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store