Nested scopes resolution -- you can breathe again!

Marcin 'Qrczak' Kowalczyk qrczak at knm.org.pl
Sat Feb 24 12:55:03 EST 2001


Sat, 24 Feb 2001 11:06:09 -0600, Russell Turpin <rturpin at do.not.use> pisze:

> When a programmer writes the variable "x," they had better know,
> in that context, to what it refers.

How do you describe the "what"? It's just the one that is bound
in the nearest enclosing function. There are no explicit variable
declarations in Python, so nested scopes make it exactly as explicit
as needed and practical under this assumption.

A requirement to qualify each variable with the full path to its
definition (__builtin__, global, functions etc.) would be impractical.

When a variable is not defined but referenced in some functions, what
would you want it to refer to? An obvious candidate is to make it
the same variable as one level up - I didn't tell Python that I want
this name to refer to anything else, so it should stay the same in the
nested function. Making it silently refer to a toplevel definition is
evil, error-prone, counter-intuitive, and I am glad that it's changing.

> And far from obscuring the intent, perhaps the SINGLE, BEST practice
> a programmer can employ, to help those who come later understand
> their work, is to put a manifest of all variables at the top of
> each function, class, and module.

Not at all. You don't manifest names you refer to which are defined
at the module level, nor builtin names, nor you don't explicitly state
that they are local. Why would you make an exception for names bound
in outer but not global scopes?

-- 
 __("<  Marcin Kowalczyk * qrczak at knm.org.pl http://qrczak.ids.net.pl/
 \__/
  ^^                      SYGNATURA ZASTĘPCZA
QRCZAK



More information about the Python-list mailing list