Pages

Wednesday, June 3, 2020

Python Anonymous Functions

Most of the time we write functions to reuse the functionality again and again. But there can be some cases where we don't need to define a function if we need to use it only once. Rather than defining a function we can define an anonymous function. If the function is only used once, or a limited number of times, an anonymous function may be syntactically lighter than using a named function

An Anonymous function or lambda expression in python is a function definition that is not bound to an identifier. In this article, we will see how lambda expressions work in python.

A Lambda function is a small anonymous function that can take any number of arguments, but can only have one expression. This function is generally defined without a name. In python a function is defined with the keyword “def” but anonymous functions are defined using a lambda function.

The basic syntax for a lambda looks as, lambda arguments : expression

A double function in python is written as,
def double(x):
      return x * 2

The above function can be simply written as 
# double = lambda x: x * 2 

We can call the lambda function as double(2) which returns the value. Similarly another simple lambda functions includes as
# x = lambda a : a + 10 , Call the function as print(x(10))
# y = lambda a,b : a * b, call the function as print y(1,2)
# z = lambda a,b,c : a * b + c , Call the function as print z(1,2,3)

Lambda inside a function - A lambda function can be defined inside a function as below,

# def myFun(n):
         return lambda a : a * n

Now the function can be called as,
# doubler=myFun(3)
# print doubler(2)

The function myFun(3) is defined and assigned to doubler, then the doubler(2) is called.

Higher Order Functions - A function that takes in other functions as arguments are called higher order functions. Python has 2 higher order functions map() and filter(). 

Filter() : The filter function in python takes a function and a list of arguments. The function is then called with all the items in the list by comparing with an expression and finally returns a modified list. The modified list contains elements which are returned by evaluating the expression on each of the elements and once the evaluation is returned true.

An Example includes,
# my_list = [ 2,4,6,1,76,34,56,77]
# my_filter_list = list(filter(lambda x: (x%2 == 0), my_list ))

My_filter_list contains all the elements returned by checking each element in my_list with the expressions (x%2 == 0). Once the return of this expression is true, it is then added to the new list.

Map() : Map function also takes a list and an expression. The function is called with all the items in the list and a new list is returned which contains items returned by that function for each item. An example include,

# my_list = [1, 5, 4, 6, 8, 11, 3, 12]
# new_list = list(map(lambda x: x * 2 , my_list))

In the above function, each element of the my_list is mapped with expressions like (1*2) and then value is passed to x which in turn passed to the new_list.

Hope this helps in understanding Anonymous and higher order functions.

No comments :

Post a Comment