Mindboggling Scope Issue
tjreedy at udel.edu
Mon Oct 25 00:26:31 CEST 2004
"James Stroud" <jstroud at mbi.ucla.edu> wrote in message
news:200410241346.12230.jstroud at mbi.ucla.edu...
> I originally posted this to the help list, but I wanted to try a broader
> audience. Its so bizarre, that it may take some discourse to understand
> is going on.
Here is a start.
> Far below are two alternative methods I have made inside a class. In
> I create a name called "passWindow.result" and use it in the "ok()"
> In "method2", I simply call this name "result".
1. In Python terms, "passWindow.result" is not a name but a dotted name
(attribute reference) consisting of two names. Perhaps you know that, but
the difference is important when it comes to scopes and name lookup, which
is the crux of your question here.
2. Your code is longer that people usually post, which is longer than some
are willing to read for free -- especially when there are two versions that
one must mentally diff by scrolling back and forth. Inserting the changed
lines of method 2 into method 1 as comments would make the differences much
easier to see.
> Testing Matt's explanation, I began a fresh CLI session (using the same
> executable used for my program). The session is cut and pasted from my
> % python
> Python 2.3.3 (#2, Feb 17 2004, 11:45:40)
> [GCC 3.3.2 (Mandrake Linux 10.0 3.3.2-6mdk)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>>>> def outer():
> ... def inner():
> ... print bob
> ... bob = "wuzzup?"
> ... inner()
> This works as I expect and in accord with Matt's explanation and the Laws
> Common Sense, but it appears to be entirely inconsistent with the
> behavior of
> "method2" below. Thus, I am completely bewildered.
> def ok():
> # GETS ERROR!
3. But what is the behavior that you are labelling 'ERROR'? Does method2
run but produce something other than what you expect? If so, please give
an example (or better, examples) of an expectation and result that diverge.
Or does method2 raise an uncaught exception? If that is so, please give
Terry J. Reedy
More information about the Python-list