Local scope for statement blocks
On Tue, Mar 4, 2014 at 2:39 PM, Brian Nguyen
do: nonlocal x k = init for i in list: k = f(i,k) n = f2(n,k)
You may need to elaborate a bit, especially as you have a declaration regarding 'x' and never use x anywhere. But if, as I suspect, you intend for this to have local k/i/n and fetch init/list/f/f2 from its parent scope, then this is something that's been discussed a few times. With the current implementation of CPython, the easiest and cleanest way to do this is with a closure. You could spell 'do' like this: def do(f): f() @do def _(): nonlocal x k = init for i in list: k = f(i,k) n = f2(n,k) Put that inside a function and it'll do most of what I think you're saying. ChrisA
On Mar 3, 2014, at 19:39, Brian Nguyen
do: nonlocal x k = init for i in list: k = f(i,k) n = f2(n,k)
You want _every_ compound statement to be a scope, or you just want to add a new one, a "do" statement, and only that new one is a scope? If the latter, i believe you can do this pretty easily with a MacroPy macro (which will expand to a def and a function call). It might be worth playing with that to get some real-life examples of using it.
participants (3)
-
Andrew Barnert
-
Brian Nguyen
-
Chris Angelico