Nonlocal: meaning, definitions and examples

🌐
Add to dictionary

nonlocal

 

[ nɒnˈloʊkəl ]

Adjective
Context #1 | Adjective

programming scope

In programming, 'nonlocal' refers to a variable that is not local to the current function or block of code but is defined in an enclosing scope. This keyword allows you to modify a variable from an outer function in a nested function, providing a way to access variables from a different scope that is not the global scope.

Examples of usage

  • The nonlocal keyword gives access to outer variables.
  • You can use nonlocal to change the value of an enclosing scope variable.
  • In Python, nonlocal helps manage variable scope in nested functions.

Etymology

The term 'nonlocal' originated in the context of programming languages to address issues related to variable scope. It first appeared in the Python programming language with its introduction of the 'nonlocal' keyword in version 3.0, released in December 2008. The 'nonlocal' keyword was designed to allow developers to easily modify variables that are defined in an enclosing scope, effectively bridging the gap between local and global variable access. Its usage has since been adapted in various programming practices and discussions surrounding variable scope. The term combines 'non-' meaning 'not' and 'local', which means 'pertaining to a limited area or scope'. This underscores the essence of accessing variables that exist outside of the immediate function but are still part of the active scope.