Created
August 22, 2013 04:07
-
-
Save anonymous/6303131 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class A(object): | |
def hook(self, f): | |
atime = 0 | |
def intime(*args): | |
print atime | |
atime += 1 | |
return f(*args) | |
return intime | |
>>> f = A().hook(lambda b: b + 1) | |
>>> f(1) | |
Traceback (most recent call last): | |
File "<stdin>", line 1, in <module> | |
File "<stdin>", line 5, in intime | |
UnboundLocalError: local variable 'atime' referenced before assignment | |
>>> |
Incidentally, when people complain about python not having proper closure support, this is (sometimes) what they mean.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks @sjl. it makes sense now. surprised I've never run in to this before.
@mattrobenolt came up with an alternate soltution: https://gist.github.com/mattrobenolt/6303154
here's the correct way to do it
and the python 3 way. bout time we start using it I think.
and javascript for the fuck of it. and because someone once told me js and python had the same scope behavior.