Message60540
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 |
|
Date |
User |
Action |
Args |
2008-01-20 09:57:04 | admin | link | issue997912 messages |
2008-01-20 09:57:04 | admin | create | |
|