A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from http://mail.python.org/pipermail/python-dev/attachments/20110509/2fd5298b/attachment.html below:

<div dir="ltr">Hi all,<br><br>It&#39;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 &#39;x&#39; referenced before assignment<br>

<br>On the usage of &#39;x&#39; 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 &#39;x = 1` and marks &#39;x&#39; as local in foo. Then, the code generator generates LOAD_FAST for all loads of  &#39;x&#39; in &#39;foo&#39;, even though &#39;x&#39; is actually bound locally after the first print. When the bytecode is run, since it&#39;s LOAD_FAST and no store was made into the local &#39;x&#39;, ceval.c then throws the exception.<br>

<br>On first sight, it&#39;s possible to signal that &#39;x&#39; truly becomes local only after it&#39;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 &quot;x is local in this block&quot;, but rather should attach scope information to each instance of &quot;x&quot;. This has some overhead, but it&#39;s only at the compilation stage so it shouldn&#39;t have a real effect on the runtime of Python code. This is also less convenient and &quot;clean&quot; than the current approach - this is why I&#39;m wondering whether the behavior is an artifact of the implementation.<br>

<br>Would it not be worth to make Python&#39;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&#39;s in line with the zen of Python to behave more explicitly here.<br>

<br>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></div>



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4