Can local function access local variables in main program?

Antoon Pardon apardon at forel.vub.ac.be
Thu Nov 8 09:51:18 EST 2007


On 2007-11-03, Dennis Lee Bieber <wlfraed at ix.netcom.com> wrote:
> On Sat, 03 Nov 2007 17:30:45 -0000, Sullivan WxPyQtKinter
><sullivanz.pku at gmail.com> declaimed the following in comp.lang.python:
>
>> Actually I am quite satisfied with and error, which is my expectation.
>> But the implicit global variable access seems quite uncomfortable to
>> me. Why is that necessary?
>
> 	Would you want to have to write things like:

Well it would be explicit and explicit is better than implicit.

> import os
> import os.path
> import sys
>
> def dFunc(more): 
> 	return "The full path of the item is: %s" % more
>
> def aFunc(something):
> 	global os.path
> 	global sys
> 	global dFunc
> 	sys.stdout.write(dFunc(os.path.join("nonsense", something)))
>
> 	Your "variable" follows the same logic used for name look ups of all
> items -- "read" access will, after exhausting the local scope, search
> the module level names (note that you don't need "global" to modify a
> mutable object in module level -- it is only the rebinding of the name
> itself that needs "global")

-- 
Antoon Pardon



More information about the Python-list mailing list