October 2018
Intermediate to advanced
420 pages
10h 26m
English
Closures are another feature that are heavily used in functional programming. A closure is the fact that a function can capture the value of a variable in one of its parent scopes. Let's see what this means in the following example:
def action(): print(value)value = "hello" action()
First, the action function is declared. This function prints the value of value even though value is neither a parameter nor a local variable. So how can this work? When the action function is being called and the print expression is executed, the interpreter searches for a reference of the value variable from the inner scope, up to the outermost scope. The interpreter searches in this order:
There ...