Closure

say it: “KLOH-zher” /ˈkloʊʒər/

An inner function that remembers the variables around it.

What is a closure?

A closure is an inner function that remembers the variables around it, even after the outer function has finished.

def make_discount(percent):
    def apply(price):
        return price * (1 - percent / 100)   # remembers percent
    return apply

ten_off = make_discount(10)
print(ten_off(50))  # 45.0

Level: Freelancer · More Python terms at this level

Updated

In other subjects