code first:
# case01
def x():
if False:
#x2 = 22
print x1
else:
print x2
if __name__ == '__main__':
if False:
x1 = 1
else:
x2 = 2
x()
case01's output:
2
No problem! but when i uncomment #x2 = 22
in if False:
block and rerun, it will got error:
---------------------------------------------------------------------------
UnboundLocalError Traceback (most recent call last)
<ipython-input-4-e36cb32b2c83> in <module>()
11 else:
12 x2 = 2
---> 13 x()
<ipython-input-4-e36cb32b2c83> in x()
4 print x1
5 else:
----> 6 print x2
7
8 if __name__ == '__main__':
UnboundLocalError: local variable 'x2' referenced before assignment
As i see, if False:
block will not excute, But why x2 = 22
take some effect to the script i worte?
My python version: 2.7.13