Swaroop
3 min readJan 11, 2018

First Class Functions In Python

If you have been using Python for some time, you certainly would have come across the phrase “Functions are First class citizens in Python”. If you are wondering what that means and wondering what about other citizens, then this article is for you.

Python Logo

Functions as first class citizens means that, you can pass functions around just like other objects. That means, you can assign functions to variables, you can pass them along as arguments, you can store them in bigger data structures, define them inside another function and also return them from another function, just like any other objects.

This is achieved in Python because functions are nothing but objects. It has so many parameters/methods in it which you can inspect using the dir() method.

def hello_func():
print("hello")

print(dir(hello_func))

And the output is:

['__annotations__', '__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__dir__', 
'__doc__', '__eq__', '__format__', '__ge__', '__get__', '__getattribute__', '__globals__', '__gt__', '__hash__', '__init__',
'__init_subclass__', '__kwdefaults__', '__le__', '__lt__', '__module__', '__name__', '__ne__', '__new__', '__qualname__',
'__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']

So, Functions are objects of type function.

Let’s see the advantages of functions being objects.

Assigning Functions To Variables

Let’s say I define a method greet like this:

def greet(name):
print("Hello " + name)

greet("World") ## => "Hello World"
greet("Universe") ## => "Hello Universe"

But Since greet is a function object, I can assign it to a variable which I call say_hello

say_hello = greet

Now say_hello is also a functions object which means, I can call it just like greet.

say_hello("Earth")                         ## => "Hello Earth"
say_hello("Mars") ## => "Hello Mars"

You can do this because functions are first class citizens.

Passing Functions As Arguments

You can pass functions as arguments to other functions as well.

Let’s say you have a call_func which is defined like this:

def call_func(x, func):
print(func(x))

The func is a function and you're just printing the value of the func(x).

Now, Let’ say I’ve two functions double and triple that are defined like this:

def double(n): return 2*n

def triple(n): return 3*n

Now you can pass these functions to call_func that we have defined earlier.

call_func(4, double)     ## => 8
call_func(5, triple) ## => 15

Note: You are passing double and triple to the function and not double() and triple()

Storing Functions In Other Data Structures

You can also store functions in other data structures like Lists, Dictionaries and more. Depending on how you implement them these can be extremely useful.

I can define an list called operations that stores the previously defined double and triple methods.

operations = [double, triple]

And after that I can call these functions with their indices.

operations[0](2)     ## => 4 
operations[1](10) ## => 30

So this way, functions are much more useful in python than in some other languages where this is not the case. These also have some good uses like generators. We’ll cover those in future articles.

That is all for this article.

This is the 20th article as part of my twitter challenge #30DaysOfBlogging. Ten more articles on various topics, including but not limited to, Java, Git, Vim, Software Development, Python, to come.

If you are interested in this, make sure to follow me on Twitter @durgaswaroop. While you’re at it, Go ahead and subscribe here on medium and my other blog as well.

If you are interested in contributing to any open source projects and haven’t found the right project or if you were unsure on how to begin, I would like to suggest my own project, Delorean which is a Distributed Version control system, built from scratch in scala. You can contribute not only in the form of code, but also with usage documentation and also by identifying any bugs in its functionality.

Thanks for reading. See you again in the next article.