AI engineering glossary · Python advanced

What is closure?

Closure: A function that remembers variables from the enclosing scope in which it was defined, even after that outer function has finished. It carries private state without needing a class. This is how decorators and factories keep configuration between calls.

Level: intermediateTopic: Python advanced

Closure: an example

def make_counter(): n = 0; def inc(): nonlocal n; n += 1; return n; return inc gives a counter that remembers n between calls.

Related terms

More Python advanced terms

Where you learn it

Closure is part of the AI Engineering Program, which teaches it with notes, a hands-on lab and an exam. The first lesson is free.

Before you go

Take the cheat sheet with you

One free PDF: the key AI-engineering terms, formulas and Python idioms. No spam; unsubscribe any time.