"GvR" == Guido van Rossum email@example.com writes:
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?
GvR> Without compiler support for this construct we have no hope of GvR> getting references to outer non-global scopes right. E.g.
GvR> def f(): GvR> x = 12
GvR> def g(): GvR> return "x is $x".sub()
GvR> return g
GvR> Here the compiler has no clue that g references x, so it GvR> wouldn't do the special treatment for x that's needed to make GvR> it work.
GvR> I see no way to fix this in general without introducing new GvR> syntax; note that the string "x is $x" could have been an GvR> argument to g().
If Python had macros, then we could define the interpolation function as a macro. It would expand to explicit references to all the variables in the block that called the macro. Then the compiler could do the right thing.
Of course, we ain't got macros, but whatever. I think they would provide cleaner support for interpolation than sys._getframe().