To install click the Add extension button. That's it.

The source code for the WIKI 2 extension is being checked by specialists of the Mozilla Foundation, Google, and Apple. You could also do it yourself at any point in time.

4,5
Kelly Slayton
Congratulations on this excellent venture… what a great idea!
Alexander Grigorievskiy
I use WIKI 2 every day and almost forgot how the original Wikipedia looks like.
Live Statistics
English Articles
Improved in 24 Hours
Added in 24 Hours
Languages
Recent
Show all languages
What we do. Every page goes through several hundred of perfecting techniques; in live mode. Quite the same Wikipedia. Just better.
.
Leo
Newton
Brights
Milds

Non-local variable

From Wikipedia, the free encyclopedia

In programming language theory, a non-local variable is a variable that is not defined in the local scope. While the term can refer to global variables, it is primarily used in the context of nested and anonymous functions where some variables can be in neither the local nor the global scope.

In Lua they are called the upvalues of the function.[1]

YouTube Encyclopedic

  • 1/3
    Views:
    5 381
    11 127
    525
  • Python - Global, Local and Nonlocal Variables
  • Python Tutorial for Beginners 55 - Global, Local and Nonlocal variables in Python
  • [24] Global, Local, Nonlocal Variables in Python | Python for Beginners

Transcription

Examples

Nested functions

In the Python 3 example that follows there is a nested function inner defined in the scope of another function outer. The variable x is local to outer, but non-local to inner (nor is it global):

def outer():
    x = 1
    def inner():
        nonlocal x
        x += 1
        print(x)
    return inner

In Javascript, the locality of a variable is determined by the closest var statement for this variable. In the following example, x is local to outer as it contains a var x statement, while inner doesn't. Therefore, x is non-local to inner:

function outer() {
    var x = 1;
    function inner() {
        x += 1;
        console.log(x);
    }
    return inner;
}

Anonymous functions

In the Haskell example that follows the variable c is non-local in the anonymous function \x -> x + c:

outer = let c = 1 in map (\x -> x + c) [1, 2, 3, 4, 5]

Implementation issues

Non-local variables are the primary reason it is difficult to support nested, anonymous, higher-order and thereby first-class functions in a programming language.

If the nested function or functions are (mutually) recursive, it becomes hard for the compiler to know exactly where on the call stack the non-local variable was allocated, as the frame pointer only points to the local variable of the nested function itself and there can be an arbitrary number of activation records on the stack in between. This is generally solved using access links or display registers.

If the nested function is passed as an argument to a higher-order function a closure needs to be built in order to locate the non-local variables. If the nested function is returned as a result from its outer function (or stored in a variable) the non-local variables will no longer be available on the stack. They need to be heap allocated instead, and their lifetime extends beyond the lifetime of the outer function that declared and allocated them. This generally requires garbage-collection.

Notes

References

This page was last edited on 12 May 2022, at 00:08
Basis of this page is in Wikipedia. Text is available under the CC BY-SA 3.0 Unported License. Non-text media are available under their specified licenses. Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc. WIKI 2 is an independent company and has no affiliation with Wikimedia Foundation.