[Python-3000] Draft PEP for outer scopes

Ka-Ping Yee python at zesty.ca
Thu Nov 2 05:28:13 CET 2006


Ka-Ping Yee wrote:
> Would it help at all to survey some folks to see how many interpret
> "global variable" to mean "top-level" vs. "anything nonlocal"?

Steven Bethard wrote:
> I don't think that'll really be worth it.  I'd be amazed if people
> didn't expect it to mean "top-level".

If that's as obvious to you as it is to me, i don't understand why
there's still any question.  I've never heard of a programming
language or conversation about programming where "global variable"
means a variable bound in an outer enclosing function; it always
means a variable bound outside of any functions (Wikipedia: "a
variable that does not belong to any subroutine or class").

> The real question is, if people see something like this::
[...]
> what would they expect it to do?

I think a fairer survey example would be something like this:

    n = 1
    def f():
        n = 2
        def g():
            global n = 99
            return n
        g()

    f()
    print n

Which 'n' do you expect g() to change?

(My answer: the global n.  That's what it says: "global n".
Which n is global?  Clearly the first one.)


-- ?!ng


More information about the Python-3000 mailing list