AI engineering glossary · Python advanced

What is nonlocal?

nonlocal: A keyword that tells a nested function that an assigned variable refers to one in the nearest enclosing function, not a new local one. It lets inner functions update outer state, such as counters in a closure.

Level: intermediateTopic: Python advanced

nonlocal: an example

Inside inner(), nonlocal total then total += x updates total from the outer function.

Related terms

More Python advanced terms

Where you learn it

nonlocal 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.