Steven D'Aprano dixit (2011-06-16, 10:46):
Making locals unrebindable is a change of semantics that is far beyond anything I've been discussed here. This will be a big enough change without overloading it with changes that will be even more controversial!
That variant (#1) would be simply shortcut for a closure application -- nothing really new.
def factory(n): """Today's Python example.""" closuring = 'foo' def func(m): s = min(n, m) * closuring # here you also cannot rebind 'closuring' because is has # been referenced above
Introducing magic syntax that is recognised by the compiler but otherwise is not usable as a function is completely unacceptable.
Because of?... And it would not be more 'magic' than any other language syntax construct -- def, class, decorating with their @, *, ** arguments etc. The fact that such a new syntax would be similar to something already known and well settled (decorator function application syntax) would be rather an andantage than a drawback.
- If the original function previously referenced global or nonlocal x
and y, the new function must now treat them as local;
- Bindings to x and y should occur once, at function definition time,
similar to the way default arguments occur once;
- The original function (before the decorator applies) must be
untouched rather than modified in place.
This implies to me that inject must copy the original function and make modifications to the code object. This sounds to me that a proof-of-concept implementation would be doable using a byte-code hack.
That's what I propose as variant #2. But that would need byte code hacking -- or some core language ('magic') modifications.