Python Lambdas : When to use and when not

 

 

 

Python lambda functions should NOT be regarded as special functions as apart from the fact that they are anonymous and have a single expression. As such, they should only be carefully used lest a misplaced use results in performance lag.

Internal handling

Python compiler treats lambda functions similar to regular functions. Following bytecode ie compiler output proves that low-level instructions generated for both are same.

def increment(x,y):
    return x+y

print("Lambda bytecode=>")
print(dis.dis(lambda x, y:x+y))
print("Defined function bytecode=>")
print(dis.dis(increment))
"""
Output:
Lambda bytecode=>
  7           0 LOAD_FAST                0 (x)
              2 LOAD_FAST                1 (y)
              4 BINARY_ADD
              6 RETURN_VALUE
None
Defined function bytecode=>
  4           0 LOAD_FAST                0 (x)
              2 LOAD_FAST                1 (y)
              4 BINARY_ADD
              6 RETURN_VALUE
None
"""

 

Example of lambda degrading performance

In the below example, apparently lambda function is affecting the map() implementation compared to list comprehension approach. For builtin functions, map still continues to perform better than list comprehension.

import timeit

iterable = list(range(100))

print("Map vs LC :: When using lambda")
map_test = 'list(map(lambda x: x*x, ' + str(iterable) + '))'
lc_test = '[x*x for x in ' + str(iterable) + ']'

map_time = timeit.timeit(map_test, number=10000)
lc_time = timeit.timeit(lc_test, number=10000)

print("Latency of map function(seconds):\t" + str(map_time))
print("Latency of list comprehension(seconds):\t" + str(lc_time))

print("*"*45)

print("Map vs LC :: When using builtin function")
map_test = 'list(map(str, ' + str(iterable) + '))'
lc_test = '[str(x) for x in ' + str(iterable) + ']'

map_time = timeit.timeit(map_test, number=10000)
lc_time = timeit.timeit(lc_test, number=10000)

print("Latency of map function(seconds):\t" + str(map_time))
print("Latency of list comprehension(seconds):\t" + str(lc_time))

"""
Output:
Map vs LC :: When using lambda
Latency of map function(seconds):	0.09578955699907965
Latency of list comprehension(seconds):	0.045968608999828575
*********************************************
Map vs LC :: When using builtin function
Latency of map function(seconds):	0.13932589599789935
Latency of list comprehension(seconds):	0.17192654799873708
"""

 

When to NOT use

  •  When there is a suitable builtin function, use it instead of lambda.
  • Not to be used as a target function of a multiprocessing Process object as it cannot be pickled ie serialized.
  • Not to be used when error trace back  is of particular importance.

When to use

  • When single expression function references need to be created and passed.
  • Need to combine with filter(), reduce(), map()

Comments

Popular Posts