Issue 997912: Enclosing Scope missing from namespace in Tutorial (original) (raw)

In the docs to Python 2.3.4 (#53, May 25 2004, 21:17:02), Section 4.6 of the Python Tutorial says:

The execution of a function introduces a new symbol table used for the local variables of the function. More precisely, all variable assignments in a function store the value in the local symbol table; whereas variable references first look in the local symbol table, then in the global symbol table, and then in the table of built-in names. Thus, global variables cannot be directly assigned a value within a function (unless named in a global statement), although they may be referenced.

This doesn't make it clear that in the following sort of case, the nested def can 'see' the varriables in the topmost function:

spam = 1 def foo(): spam = 2 ham = 3 def bar(): print spam, ham bar()

foo() 2 3

I suggest the following ammendment:

. . . whereas variable references first look in the local symbol table, then in the local scope of the enclosing function defs (if any), then in the global symbol table, . . .

Thanks,

Brian vdB

Logged In: YES user_id=1015686

Hi,

I'm new to SF. I meant the e.g. to read like:

spam = 1 def foo(): ....spam = 2 ....ham = 3 ....def bar(): ........print spam, ham ....bar()

foo() 2 3

(where '.' are spaces.)

Thanks,

brian