Can anyone explain why "begin B: 123" prints, but 456 doesn't? $ /usr/bin/python2.3 x1x2.py begin A: Pre B: 123 456 begin B: 123 Traceback (most recent call last): File "x1x2.py", line 13, in ? A() File "x1x2.py", line 11, in A B() File "x1x2.py", line 7, in B print "begin B:",x1,x2 UnboundLocalError: local variable 'x2' referenced before assignment
$ cat x1x2.py #!/usr/bin/env python def A(): print "begin A:" x1=123; x2=456; def B(): print "begin B:",x1,x2 x2 = x2 - 1; # comment out this line and script x1x2 magically works!! print "end B:",x1,x2 print "Pre B:",x1,x2 B() print "end A:",x1,x2 A() $ /usr/bin/python2.3 Python 2.3.4 (#1, Mar 10 2006, 06:12:09) [GCC 3.4.5 20051201 (Red Hat 3.4.5-2)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> $ /usr/bin/python2.3 -V Python 2.3.4 $ /usr/local/bin/python2.4 -V Python 2.4.2 $ /usr/local/bin/python2.4 x1x2.py begin A: Pre B: 123 456 begin B: 123 Traceback (most recent call last): File "x1x2.py", line 13, in ? A() File "x1x2.py", line 11, in A B() File "x1x2.py", line 7, in B print "begin B:",x1,x2 UnboundLocalError: local variable 'x2' referenced before assignment # I compiled up 2.4 from the FC4 source, but 2.3 was from SL4.3 $ python -V Python 2.4.2 $ python Python 2.4.2 (#1, Aug 15 2006, 21:51:33) [GCC 3.4.5 20051201 (Red Hat 3.4.5-2)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> $ -- http://mail.python.org/mailman/listinfo/python-list