nested scopes

Thomas Wouters thomas at xs4all.net
Fri Feb 2 13:46:09 EST 2001


On Fri, Feb 02, 2001 at 04:05:58PM +0000, Robin Becker wrote:
> In article <Pine.SOL.4.30.0102021550090.28216-100000 at mimosa.csv.warwick.
> ac.uk>, John J. Lee <phrxy at csv.warwick.ac.uk> writes
> >On Fri, 2 Feb 2001, Robin Becker wrote:
> >
> >> Is it really true that J Hylton's nested scopes will cause code like
> >>
> >> def bingo():
> >>   from string import *
> >>   ....
> >>
> >> to be declared bad? I'm already against nested scopes, but requiring
> >> this to be wrong seems awful. We seem to be replacing a simple 2-level
> >> system by a more complex one & restrictions.
> >[...]
> >
> >Are nested scopes actually going to end up in Python proper, then?  As
> >opposed to remaining as an optional patch?

> don't know about that, apparently 2.1a has it

It won't be optional, it'll be part of Python. I guess that if there is
a *really* *really* *really* lot of code out there that will break, it could
be withheld until Python 2.2, and the practice of 'from foo import *' in
function/class scopes be made to issue a warning in Python 2.1, but frankly
I doubt there'll be that much code breakage. At least it's not a *silent*
breakage ! It's a compiler error, so the first run of a script under the new
Python will detect it. It's not like it's a new kind of exception being
raised.

> >And why should the above become bad rather than just import into the
> >appropriate namespace?
> >
> ...
> I have a feeling that in order to make nested scopes workable/acceptable
> the byte compiler is being hacked and this has had some unfortunate side
> effects.

Now, now, 'hacked' is a bit strong. Jeremy did a fairly extensive rewrite of
the compiler, complete and decent enough not to be called a 'hack' for sure.
Besides, developers never 'hack', they 'extend' :-) The problem with 'from
foo import *' in a function or class scope, though, is that it makes it
impossible for the compiler to see what names will be defined in that scope,
and hence what names a nested scope uses will resolve to that. I don't like
it at all, either. In fact, I was the first to whine about it, incessantly,
on python-dev. I think I have to buy Jeremy a beer (or whatever's his
poison) on the Python conference to make up ;)

The situation does lend itself to a workaround (a *true* hack, mind
you:) but I haven't looked into implementing it, yet. (Jeremy might be doing
that, but I'm not sure): The compiler could try and live with 'from foo
import *' as long as the function doesn't define a scope nested in that
function. Unfortunately, this isn't going to fix every instance of the
problem: a single lambda statement inside the function will make the import
illegal again. Disabling nested-scoping in the presense of 'from foo import
*' might be technically possible, but would create enormous amounts of
confusion, I'm sure.

And lastly, I'd like to point out (as Jeremy does :) that the reference
manual already states that 'from foo import *' in anything but a module
scope is illegal. Even if the nested scopes patch was withdrawn from Python
2.1, 'from foo import *' in local scopes *will* generate a warning (warnings
are also new in 2.1) so your best bet, either way, is sanitizing your code. 

(Don't get me wrong: I seriously think 'from foo import *' is overused. The
language reference makes it pretty clear what it was intended for, and I
think even the common 'from Tkinter import *' is wrong use -- but I don't
like code breakage, regardless.)

-- 
Thomas Wouters <thomas at xs4all.net>

Hi! I'm a .signature virus! copy me into your .signature file to help me spread!




More information about the Python-list mailing list