This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Enclosing Scope missing from namespace in Tutorial
Type: Stage:
Components: Documentation Versions: Python 2.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: georg.brandl Nosy List: brianvanden, georg.brandl
Priority: low Keywords: easy

Created on 2004-07-26 09:39 by brianvanden, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (3)
msg60540 - (view) Author: Brian vdB (brianvanden) Date: 2004-07-26 09:39
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
msg60541 - (view) Author: Brian vdB (brianvanden) Date: 2004-07-26 18:15
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
msg61411 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2008-01-21 16:52
Fixed in r60158.
History
Date User Action Args
2022-04-11 14:56:05adminsetgithub: 40644
2008-01-21 16:52:00georg.brandlsetstatus: open -> closed
resolution: fixed
messages: + msg61411
2008-01-20 19:13:43christian.heimessetpriority: normal -> low
assignee: georg.brandl
nosy: + georg.brandl
keywords: + easy
versions: + Python 2.6, - Python 2.3
2004-07-26 09:39:38brianvandencreate