At 07:32 AM 5/1/2006 -0700, Guido van Rossum wrote: >On 4/30/06, Ben Wing <ben at 666.com> wrote: > > [1] ideally, change this behavior, either for 2.6 or 3.0. maybe have a > > `local' keyword if you really want a new scope. > > [2] until this change, python should always print a warning in this > > situation. > > [3] the current 'UnboundLocal' exception should probably be more > > helpful, e.g. suggesting that you might need to use a `global foo' > > declaration. > >You're joking right? While I agree that item #1 is a non-starter, it seems to me that in the case where the compiler statically knows a name is being bound in the module's globals, and there is a *non-argument* local variable being bound in a function body, the odds are quite high that the programmer forgot to use "global". I could almost see issuing a warning, or having a way to enable such a warning. And for the case where the compiler can tell the variable is accessed before it's defined, there's definitely something wrong. This code, for example, is definitely missing a "global" and the compiler could in principle tell: foo = 1 def bar(): foo+=1 So I see no problem (in principle, as opposed to implementation) with issuing a warning or even a compilation error for that code. (And it's wrong even if the snippet I showed is in a nested function definition, although the error would be different.) If I recall correctly, the new compiler uses a control-flow graph that could possibly be used to determine whether there is a path on which a local could be read before it's stored.
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