[Python-Dev] Lexical scoping in Python 3k

Josiah Carlson jcarlson at uci.edu
Sat Jul 1 11:01:12 CEST 2006

Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
> Josiah Carlson wrote:
> > What I asked before, and what I'd like to ask again, is if there are any
> > _nontrivial uses_ of lexically nested scopes which are made cumbersome
> > by our inability to write to parent scopes.
> The trouble with taking that position is that the very
> cases which would benefit are very *simple* ones, where
> it would be cumbersome to refactor it to use a class,
> or mutable object in the outer scope, etc. So you've
> effectively set up your acceptance criteria to be
> unmeetable.

If the only code that benefits from such changes are "very *simple*",
then I think that says something about its necessity.  That is, if
anything more complicated than those that are "very *simple*" generally
don't benefit, then I don't believe that such a modification would be
beneficial to the language overall. Further, a simple namespace factory
can handle much of the current issues, without needing to create or
change keywords.

def namespace(**kwds):
    class namespace(object):
        __slots__ = kwds.keys()
        def __init__(self):
            for i,j in kwds.iteritems():
                setattr(self, i,j)
    return namespace()

def trivial_counter(start):
    ns = namespace(current=start-1)
    def next():
        ns.current += 1
        return ns.current
    return next

Maybe a variant of the above namespace factory should make it into the
collections module.

> > If there aren't, then I'm
> > going to again have to argue against new syntax, keywords, and their use.
> There's one very simple way we could do this in Py3k
> without requiring any new syntax or keywords: just
> redefine the meaning of "global" to mean "not local".

I would probably be a solid -0 on such a proposal; I still don't think
it's really necessary, but I've never used (or really seen) global more
than one level deep, so would guess its impact would be low.

 - Josiah

More information about the Python-Dev mailing list