Blurbs ansible coffeescript django dvcs erlang es6 hg javascript json lua mercurial peewee postgresql python scope sql sqlalchemy yaml

Globals and locals part 3 code python scope

a = 1

def g():
    print(a)
    print([a for a in range(3)])

g()

# Python 2 only (leaky comprehensions):
# >>> UnboundLocalError: local variable 'a' referenced before assignment

Globals and locals part 2 code python scope

a = 1

def f():
    a = a + 1
    # or even shorter!
    a += 1

f()

# >>> UnboundLocalError: local variable 'a' referenced before assignment

Globals and locals code python scope

item = 'spam'

def p():
    print item # <<<
    for item in ['foo', 'bar']:
        print item

p()

# >>> UnboundLocalError: local variable 'item' referenced before assignment

# variable global to a scope that you reassign within that scope
# is marked local to that scope by the compiler.

http://stackoverflow.com/questions/404534/python-globals-locals-and-unboundlocalerror

Defaults binding code python scope

for i in range(10):
    def callback(i=i):
        print "clicked button", i
    UI.Button("button %s" % i, callback)

# The “i=i” part binds the parameter “i” (a local variable)
# to the current value of the outer variable “i”.

http://effbot.org/zone/default-values.htm

Closure without return code javascript scope

var bar;
function foo(x) {
    bar = function() {
        alert(x);
    }
};
foo(5);
bar();