[Python-Dev] PEP 292, Simpler String Substitutions

Guido van Rossum guido@python.org
Wed, 19 Jun 2002 10:50:59 -0400


> > BTW, you can't use locals() or globals() because you really want
> > globals()-overridden-with-locals(), i.e.
> > 
> >     d = globals().copy()
> >     d.update(locals())
> 
> What about free/cell vars?  Will these be used?  
> If not, is that a problem?

Without compiler support for this construct we have no hope of getting
references to outer non-global scopes right.  E.g.

def f():
    x = 12

    def g():
        return "x is $x".sub()

    return g

Here the compiler has no clue that g references x, so it wouldn't do
the special treatment for x that's needed to make it work.

I see no way to fix this in general without introducing new syntax;
note that the string "x is $x" could have been an argument to g().

--Guido van Rossum (home page: http://www.python.org/~guido/)