<p>On May 9, 2011 6:59 AM, "Eli Bendersky" <<a href="mailto:eliben@gmail.com">eliben@gmail.com</a>> wrote:<br>
><br>
> Hi all,<br>
><br>
> It's a known Python gotcha (*) that the following code:<br>
><br>
> x = 5<br>
> def foo():<br>
> print(x)<br>
> x = 1<br>
> print(x)<br>
> foo()<br>
><br>
> Will throw:<br>
><br>
> UnboundLocalError: local variable 'x' referenced before assignment<br>
><br>
> On the usage of 'x' in the *first* print. Recently, while reading the zillionth question on StackOverflow on some variation of this case, I started thinking whether this behavior is desired or just an implementation artifact. <br>
><br>
> IIUC, the reason it behaves this way is that the symbol table logic goes over the code before the code generation runs, sees the assignment 'x = 1` and marks 'x' as local in foo. Then, the code generator generates LOAD_FAST for all loads of 'x' in 'foo', even though 'x' is actually bound locally after the first print. When the bytecode is run, since it's LOAD_FAST and no store was made into the local 'x', ceval.c then throws the exception.<br>
><br>
> On first sight, it's possible to signal that 'x' truly becomes local only after it's bound in the scope (and before that LOAD_NAME can be generated for it instead of LOAD_FAST). To do this, some modifications to the symbol table creation and usage are required, because we can no longer say "x is local in this block", but rather should attach scope information to each instance of "x". This has some overhead, but it's only at the compilation stage so it shouldn't have a real effect on the runtime of Python code. This is also less convenient and "clean" than the current approach - this is why I'm wondering whether the behavior is an artifact of the implementation.<br>
><br>
> Would it not be worth to make Python's behavior more expected in this case, at the cost of some implementation complexity? What are the cons to making such a change? At least judging by the amount of people getting confused by it, maybe it's in line with the zen of Python to behave more explicitly here.</p>
<p>This is about mixing scopes for the the same name in the same block, right? Perhaps a more specific error would be enough, unless there is a good use case for having that mixed scope for the name.</p>
<p>-eric</p>
<p>> Thanks in advance,<br>
> Eli<br>
><br>
> (*) Variation of this FAQ: <a href="http://docs.python.org/faq/programming.html#why-am-i-getting-an-unboundlocalerror-when-the-variable-has-a-value">http://docs.python.org/faq/programming.html#why-am-i-getting-an-unboundlocalerror-when-the-variable-has-a-value</a><br>
><br>
><br>
><br>
> _______________________________________________<br>
> Python-Dev mailing list<br>
> <a href="mailto:Python-Dev@python.org">Python-Dev@python.org</a><br>
> <a href="http://mail.python.org/mailman/listinfo/python-dev">http://mail.python.org/mailman/listinfo/python-dev</a><br>
> Unsubscribe: <a href="http://mail.python.org/mailman/options/python-dev/ericsnowcurrently%40gmail.com">http://mail.python.org/mailman/options/python-dev/ericsnowcurrently%40gmail.com</a><br>
><br>
</p>