nested scopes

Tim Peters tim.one at home.com
Fri Feb 2 23:11:50 EST 2001


[Robin Becker]
> Is it really true that J Hylton's nested scopes will cause code like
>
> def bingo():
>   from string import *
>   ....
>
> to be declared bad?

As has been pointed out, the "*" form of import has always been documented
as unsupported at any but module scope.  Sometimes Guido wishes people would
read the manuals he worked so hard to write <wink>.

But as the 2.1a2 NEWS file says,

    In a future release, the compiler may allow this form when
    there is no possibility of ambiguity.

I guess this depends on how loudly people scream.  Before screaming too
loudly, though, realize that in 2.0 and before, the mere presence of "import
*" in a function forces the compiler to generate worse (slower) code for the
entire function body, since it no longer has any idea which names are local,
and so has to treat all names as if they "might be" local.  "exec" stmts
have the same effect on the scope in which they appear.

> I'm already against nested scopes, but requiring this to be wrong
> seems awful.

Was already wrong, and it's unclear how making your code clearer and faster
is "awful".

> We seem to be replacing a simple 2-level system by a more complex
> one & restrictions.

Exactly right, except that it has been a 3-level system (which Guido called
a 2-level system in a successful attempt to make people think it's simpler
than it is <wink>).

I don't think I know of any other language trying to mix arbitrarily deeply
nested scopes with lack of declarations.  The schemes for "import *" and
"exec" were hanging by a thread before; it's doubtful they'll ever work well
with deep nesting.

I may have missed one, but I counted three checkins to repair nested use of
"import *" in the CVS tree for the 2.1a2 release.  That's not much out of
aobut ~6Mb of Python source code.  "from Tkinter import *" is the leader for
worst offender.  2/3 of the offending lines were written by Guido, 1/3 by
Barry.

so-i-suppose-guido-is-twice-as-outraged-ly y'rs  - tim





More information about the Python-list mailing list