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.
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
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
Let’s see the advantages of functions being objects.
Assigning Functions To Variables
Let’s say I define a method
greet like this:
print("Hello " + name)
greet("World") ## => "Hello World"
greet("Universe") ## => "Hello Universe"
greet is a function object, I can assign it to a variable which I call
say_hello = greet
say_hello is also a functions object which means, I can call it just like
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):
func is a function and you're just printing the value of the
Now, Let’ say I’ve two functions
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
tripleto the function and not
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
operations = [double, triple]
And after that I can call these functions with their indices.
operations(2) ## => 4
operations(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.
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.