[issue5763] scope resolving error
New submission from vid podpecan vid.podpe...@gmail.com: Consider the following two functions: def outer(): a = 1 def inner(): print a inner() #end outer() def outer_BUG(): a = 1 def inner(): print a a = 2 inner() #end outer_BUG() The first function outer() works as expected (it prints 1), but the second function ends with an UnboundLocalError, which says that the print a statement inside inner() function references a variable before assignment. Somehow, the interpreter gets to this conclusion by looking at the next statement (a = 2) and forgets the already present variable a from outer function. This was observed with python 2.5.4 and older 2.5.2. Other releases were not inspected. Best regards, Vid -- messages: 85992 nosy: vpodpecan severity: normal status: open title: scope resolving error type: behavior versions: Python 2.5 ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue5763 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue5763] scope resolving error
Mark Dickinson dicki...@gmail.com added the comment: This is not a bug, just a common gotcha. The rules are described at: http://docs.python.org/reference/executionmodel.html#naming Here's the relevant excerpt: If a name binding operation occurs anywhere within a code block, all uses of the name within the block are treated as references to the current block. This can lead to errors when a name is used within a block before it is bound. This rule is subtle. Python lacks declarations and allows name binding operations to occur anywhere within a code block. The local variables of a code block can be determined by scanning the entire text of the block for name binding operations. -- nosy: +marketdickinson ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue5763 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue5763] scope resolving error
Mark Dickinson dicki...@gmail.com added the comment: Closing as invalid. -- resolution: - invalid status: open - closed ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue5763 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com