Skip to content

Instantly share code, notes, and snippets.

@tmadlener
Last active April 4, 2018 09:09
Show Gist options
  • Save tmadlener/cae049846bdbc8855afd26c592f02044 to your computer and use it in GitHub Desktop.
Save tmadlener/cae049846bdbc8855afd26c592f02044 to your computer and use it in GitHub Desktop.
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