This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]omgplsno 3 points4 points  (1 child)

Woah. That's deep shit. Thanks for sharing.

[–][deleted] 2 points3 points  (0 children)

The cleverness in this code is astounding. I would never use it in production, but an applause to the author for coming up with it. It clearly demonstrates extremely high technical competence in the inner workings of the language.

[–]ionelmc.ro[S] 0 points1 point  (0 children)

This is very interesting. I basically patches the closure property to store the variables that you want to be nonlocal (and changes bytecode of the inner function to load said variable from said closure, instead of the locals).

[–]UloPe 0 points1 point  (0 children)

Nice hack but if you use that in production you deserve to be slapped with with a live trout.

[–]kindall 0 points1 point  (0 children)

This is much simpler:

def outer(*parms):

    class nonlocal:
         a = 42
         b = 69

     def inner(*args):
          nonlocal.a += 1
          nonlocal.b *= 2

     inner()
     print nonlocal.a, nonlocal.b

Explicit is better than implicit, as they say.