Globals and locals part 3 code
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
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
a = 1
def f():
a = a + 1
# or even shorter!
a += 1
f()
# >>> UnboundLocalError: local variable 'a' referenced before assignment
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
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”.
var bar;
function foo(x) {
bar = function() {
alert(x);
}
};
foo(5);
bar();