[Python-Dev] Those import related syntax errors again...

Thomas Wouters thomas@xs4all.net
Wed, 21 Feb 2001 23:29:38 +0100


On Wed, Feb 21, 2001 at 09:56:40AM -0500, Jeremy Hylton wrote:

> A note of clarification seems important here:  The restrictions are
> not being introduced to simplify the implementation.  They're being
> introduced because there is no sensible meaning for code that uses
> import * and nested scopes with free variables.  There are two
> possible meanings, each plausible and neither satisfying.

I disagree. There are several ways to work around them, or the BDFL could
just make a decision on what it should mean. The decision between using a
local vrbl in an upper scope or a possible global is about as arbritrary as
what 'if key in dict:' and 'for key in dict' should do. I personally think
it should behave exactly like:

def outer(x, y):
    a = ...
    from module import *
    def inner(x, y, z=a):
        ...

used to behave (before it became illegal.) That also makes it easy to
explain to people who already know the rule.

A possibly more practical solution would be to explicitly require a keyword
to declare vrbls that should be taken from an upper scope rather than the
global scope. Or a new keyword to define a closure. (def closure NAME():
comes to mind.) Lots of alternatives available if the implementation of
PEP227 can't be done without introducing backwards incompatibility and
strange special cases.

Because you have to admit (even though it's another hypothetical howl) that
it is odd that a function would *stop functioning* when you change a
lambda (or nested function) to use a closure, rather than the old hack:

def inner(x):
    exec ...
    myprint = sys.stderr.write
    spam = lambda x, myprint=myprint: myprint(x*100)

I don't *just* object to the backwards incompatibility, but also to the
added complexity and the strange special cases, most of which were
introduced (at my urging, I'll readily admit and for which I should and do
appologize) to reduce the impact of the incompatibility. I do not believe
the ability to leave out the default-argument-hack (if you don't use
import-*/exec in the same function) is worth all that.

-- 
Thomas Wouters <thomas@xs4all.net>

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