Skip to content

Instantly share code, notes, and snippets.

@rf
Created May 23, 2012 17:43
Show Gist options
  • Save rf/2776610 to your computer and use it in GitHub Desktop.
Save rf/2776610 to your computer and use it in GitHub Desktop.
'read only' closures in python
function outer () {
var foo = 'yellow';
function inner () {
foo = 'blue';
}
function out () {
console.log(foo);
}
return [inner, out];
}
var result = outer();
var inner = result[0];
var out = result[1];
out();
inner();
out();
// $ node test.js
// yellow
// blue
def outer ():
foo = 'yellow'
def inner ():
foo = 'blue' # this `foo` is local to the function `inner()`
def out ():
print foo # this `foo` is the `foo` defined on line 2
return inner, out
inner, out = outer()
out()
inner()
out()
# $ python test.py
# yellow
# yellow
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment