nested scopes

Tim Peters tim.one at home.com
Sat Feb 3 14:57:51 EST 2001


[Robin Becker]
> as I suspected this is about optimality;

Since the local vrbl optimization happened 7 years ago, it really is too
late to cry "foul!" on that one <wink>.  BTW, the *rules* for when a name is
local did *not* change then; they simply got exploited; and Python actually
got more predictable because of it (although only in obscure endcases, none
of which I saw that weren't contrived).

> speed vs clarity/simplicity is the argument here.

Sorry, but there isn't, and never was, great clarity about what "import *"
and "exec" do in all cases; that's why the Reference Manual hedged about
them even before the local vrbl optimization was conceived.  You have always
had to be a bona fide expert in Python's internals to predict their behavior
in cases where interaction with the namespaces is anything less than bloody
straightforward.  That was true before the local vrbl optimization too.

> I guess that the nested scopes stuff has just brought it to the fore.

It's still in the background for me <wink>.

> Luckily we're not forced to use nested scopes in python, pascal
> experience makes me despise them because of all that paging up
> and down to find the defining declaration.

I'll repeat something I've said dozens of times over the years in the
ongoing scope debate:  it's going to be much harder in Python, because there
aren't any "defining declarations" in Python.  You have to search up and
search down, looking at all assignments, all imports, all "for" loops
(whether or not nested in listcomps) ... everywhere a binding occurs.

Python's "2-level" scheme was a deliberate counterpoint to the abuses of
deep lexical nesting in Pascal programs of the time (according to Guido),
and it's something I've grown to like a lot.  But newbies eternally stumble
over that functions nest textually but not lexically in Python, and this
will make life simpler for them.  People mucking with excruciatingly subtle
"exec" and "import" tricks arguably deserved to be shot anyway <wink>.

not-that-a-plain-"import-*"-at-function-scope-is-excruciatingly-
    subtle-ly y'rs  - tim





More information about the Python-list mailing list