Last active
April 4, 2018 09:09
-
-
Save tmadlener/cae049846bdbc8855afd26c592f02044 to your computer and use it in GitHub Desktop.
This file contains 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
surrounding_y = None | |
class ClosureClass: | |
def __init__(self, x): | |
self.x = x | |
self.y = surrounding_y | |
def __repr__(self): | |
return '{}, {}'.format(self.x, self.y) | |
def make_closure_class(y): | |
global surrounding_y | |
surrounding_y = y | |
return ClosureClass | |
Test = make_closure_class('seems to work') | |
test = Test('this is x') | |
test2 = Test('this is another x') | |
AnotherTest = make_closure_class('Even different captures do') | |
test3 = AnotherTest('different y') | |
# This does not work since the surrounding_y already points to | |
# something different | |
test4 = Test('this breaks') | |
print test | |
print test2 | |
print test3 | |
print test4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment