[Python-Dev] LOAD_NAME & classes

Guido van Rossum guido@python.org
Tue, 23 Apr 2002 10:16:37 -0400


> [Guido van Rossum]
> > We need more than a single example to decide which rules bites worse
> > for large programs.  Deep nesting is not common; long functions are.
> > And there the common annoyance is that a change in line 150 can break
> > the code in line 2 of the function.
> 
> I'm not exactly sure what you mean by this. Can you share an
> example? (Not necessarily 150+ lines long, of course.) Thanks.

It's a classic.  Before we had UnboundLocalError (i.e. in 1.5.2 and
before) this was a common problem on c.l.py:

    x = "a global"

    def f():
        print x # user thinks this should print the global
	# 2000 lines of unrelated code
	for x in "some sequence": # doesn't realize this overrides x
            do_something_with(x)

Calling f() would raise NameError: x, which caused lots of confusion.

We added UnboundLocalError th make it clearer what's going on (so at
least the experienced c.l.py users would know right away where the
problem was :-), but still requires you to know about something
obscure that's going on at compile time (the compiler scanning your
entire function for variable definitions).

--Guido van Rossum (home page: http://www.python.org/~guido/)