scope of function parameters (take two)

Terry Reedy tjreedy at udel.edu
Mon May 30 21:30:39 EDT 2011


On 5/30/2011 8:28 PM, Henry Olders wrote:


> Sadly, I feel that the main issue that I was trying to address, has
> not been dealt with.

False. Please go back and read what I and others wrote before.

...
> What I would like is that the variables which are included in the
> function definition's parameter list, would be always treated as
> local to that function (and of course, accessible to nested
> functions)

You would like Python to be the way it is. Fine. For the Nth time,
PARAMATER NAMES ARE LOCAL NAMES. Period.

> but NOT global unless explicitly defined as global.

PARAMETER NAMES **CANNOT** BE DEFINED AS GLOBAL.

 >>> def f(a):
	global a
	
SyntaxError: name 'a' is parameter and global

Again, go back and reread what I and other wrote. I believe that you 
are, in part, hypnotized by the work 'variable'. Can you define the 
word? There are 10 to 20 possible variations, and yours is probably 
wrong for Python.

> quote from wikipedia: "It is considered good programming practice to
> make the scope of variables as narrow as feasible so that different
> parts of a program do not accidentally interact with each other by
> modifying each other's variables.

 From 'import this':
"Namespaces are one honking great idea -- let's do more of those!
Python is loaded with namespaces.

 > Doing so also prevents action at a
> distance. Common techniques for doing so are to have different
> sections of a program use different namespaces, or to make individual
> variables "private" through either dynamic variable scoping or
> lexical variable scoping."
> (http://en.wikipedia.org/wiki/Variable_(programming)#Scope_and_extent).

Python is lexically scoped.

> another quote from the wikipedia entry for Common Lisp: "the use of
> lexical scope isolates program modules from unwanted interactions.

Python is lexically scoped.

> If making python behave this way is impossible,

How do you expect us to respond when you say "Please make Python the way 
it is."? Or is you say "If making Python the way it is is impossible..."?

Now, if you actually want Python to drastically change its mode of 
operation and break most existing programs, do not bother asking.

 > Are there others who feel as I do that a
> function parameter should always be local to the function?

Yes, we all do, and they are.

-- 
Terry Jan Reedy




More information about the Python-list mailing list