Created
May 23, 2012 17:43
-
-
Save rf/2776610 to your computer and use it in GitHub Desktop.
'read only' closures in python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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