This question already has answers here:
Closed 7 years ago.
Possible Duplicate:
Short Description of Python Scoping Rules
I wrote two simple functions:
# coding: utf-8
def test():
var = 1
def print_var():
print var
print_var()
print var
test()
# 1
# 1
def test1():
var = 2
def print_var():
print var
var = 3
print_var()
print var
test1()
# raise Exception
In comparison, test1()
assigns value after print var
, then raise an Exception: UnboundLocalError: local variable 'var' referenced before assignment
, I think the moment I call inner print var
, var has a value of 2, am I wrong?