<div dir="ltr"><br><br><div class="gmail_quote">On Mon, May 9, 2011 at 18:44, Isaac Morland <span dir="ltr">&lt;<a href="mailto:ijmorlan@uwaterloo.ca">ijmorlan@uwaterloo.ca</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">

<div class="im">On Mon, 9 May 2011, Eli Bendersky wrote:<br>
<br>
</div><div><div></div><div class="h5"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">


x = 5<br>
def foo ():<br>
       print (x)<br>
       if bar ():<br>
               x = 1<br>
       print (x)<br>
<br>
</blockquote>
<br>
I wish you&#39;d annotate this code sample, what do you intend it to<br>
demonstrate?<br>
<br>
It probably shows the original complaint even more strongly. As for being a<br>
problem with the suggested solution, I suppose you&#39;re right, although it<br>
doesn&#39;t make it much different. Still, before a *possible* assignment to<br>
&#39;x&#39;, it should be loaded as LOAD_NAME since it was surely not bound as<br>
local, yet.<br>
</blockquote>
<br></div></div>
Extrapolating from your suggestion, you&#39;re saying before a *possible* assignment it will be treated as global, and after a *possible* assignment it will be treated as local?<br>
<br>
But surely:<br>
<br>
print (x)<br>
if False:<br>
        x = 1<br>
print (x)<br>
<br></blockquote><div>[snip]<br><br>Alright, I now understand the problems with the suggestion. Indeed, conditional assignments that are only really resolved at runtime are the big stumbling block here.<br><br>However, maybe the error message/reporting can still be improved?<br>

<br>ISTM the UnboundLocalError exception gets raised only in those weird and confusing cases. After all, why would Python decide an access to some name is to a local? Only if it found an assignment to that local in the scope. But that assignment clearly didn&#39;t happen yet, so the error is thrown. So cases like these:<br>

<br>x = 2<br><br>def foo1():<br>  x += 1 <br><br>def foo2():<br>  print(x)<br>  x = 10<br><br>def foo3():<br>  if something_that_didnot_happen: <br>    x = 10<br>  print(x)<br><br>All belong to the category. <br><br>With an unlimited error message length it could make sense to say &quot;Hey, I see &#39;x&#39; may be assigned in this scope, so I mark it local. But this access to &#39;x&#39; happens before assignment - so ERROR&quot;. This isn&#39;t realistic, of course, so I&#39;m wondering:<br>

<br>1. Does this error message (although unrealistic) capture all possible appearances of UnboundLocalError?<br>2. If the answer to (1) is yes - could it be usefully shortened to be clearer than the current &quot;local variable referenced before assignment&quot;?<br>

<br>This may not be possible, of course, but it doesn&#39;t harm trying :-)<br>Eli<br><br><br><br><br><br><br><br> <br></div></div><br></div>