[Python-Dev] Explicit Lexical Scoping (pre-PEP?)
Phillip J. Eby
pje at telecommunity.com
Wed Jul 5 17:53:50 CEST 2006
At 10:21 AM 7/5/2006 +0200, Guido van Rossum wrote:
>Thanks for bringing this up. I'm not sure what I think of it yet. One
>problem I see is that there might end up being two ways to reference
>variables in outer scopes: .num if you plan to assign to it, or just
>num if you only reference it. I find that the most disurbing issue so
>far; modified global declarations or outer declarations don't have
>this problem.
Well, you could make it mandatory in Py3K I suppose, though I'm not sure I
like it being mandatory, due to the frequent need to reference top-level
names that would cause an awful lot of dots to start popping up.
But for existing Python, the optional nature of the '.' allows existing
code to run unchanged. And for versions that support the new syntax, using
a leading '.' for all non-global, non-locals should be considered "good
style" since it highlights the dependency and practically shouts "tricky
stuff here, pay attention."
Ironically, having *only* one way to refer to outer variables makes it
impossible to communicate this distinction in the common read-only case.
>Would this also use ..num to refer to num in an outer scope two levels
>removed?
I think that's unnecessary; it would be much better to use variables with
distinct names.
By the way, an interesting thought for Py3K is that you could maybe use
this syntax to do away with explicit 'self', if you consider the class'
namespace to be part of a function's closure.
E.g.:
class Foo:
whee = 42
def bar(baz):
print .whee
Consider this: if Foo were a function rather than a class, each invocation
of Foo would yield a new namespace in which 'whee' is defined. However,
each invocation of a *class* also yields a new namespace. So there's a
definite symmetry in using .whee to refer to an instance attribute of Foo.
The big problem that comes to mind with that idea is that it makes it
impossible to have argument names that are the same as attribute names,
unless the 'whee'/'.whee' prohibition were relaxed. :( But it's an
intriguing thought, nonetheless.
More information about the Python-Dev
mailing list