Functions are one of the most versatile tools in any Python programmer’s toolbox. In this article, I will be discussing the different types of functions you will encounter as a Python developer. Impure Functions #impure-functions num = 0 def sq(x): global num num = x # side effect return x * x sq_num = sq(2) # returns 4 print(num) # 2 print(sq_num) # 4

The code above uses the map higher-order function in Python to square all the values in the array via an anonymous function.

Closures #closures def factory(): num = 10 def mult_by_10(inp): return num * inp return mult_by_10 clos = factory() clos(2) # returns 20

Related Articles