[Python-Dev] LOAD_NAME & classes

Tim Peters tim.one@comcast.net
Tue, 23 Apr 2002 23:53:59 -0400


[Guido]
> ...
> This particular form of breakage was a common error reported on c.l.py
> and to help at python.org until we added UnboundLocalError to make the
> diagnostic cleaner.

It was indeed, and adding UnboundLocalError did cut the number of questions.

> Maybe that's all that's needed;

It's hard to know what could really help more.  If Greg Wilson is still
running newcomer experiments, I'd like to see what newcomers have to say
about this:


x = 2
def f():
    print x  # A
    x = 3

f()
print x      # B


A:  What do you think should happen when the print at A executes?
B:    "   "  "    "      "      "     "   "    "    " B     "   ?

What I suspect, but don't know, is that a majority of newcomers who expect A
to print 2 *also* expect B to print 3.  That is, that they're thinking x is
a global variable, and have no conception of local variables in mind.

This is actually what happens in Icon, which also lacks declarations (in the
same sense Python lacks them:  it doesn't lack them <wink>).  The difference
is that all variables are global by default in Icon, and you need to
explicitly say "local x" if you want a local var instead.  That's
error-prone for sure, by not quite as much so as Perl (where x is also
global by default, but "local $x" sucks you into dynamic scoping; it does
not in Icon).