Skip to content

Instantly share code, notes, and snippets.

@nakamuray
Created January 23, 2014 14:31
Show Gist options
  • Save nakamuray/8579365 to your computer and use it in GitHub Desktop.
Save nakamuray/8579365 to your computer and use it in GitHub Desktop.
python で closure 使って class みたいなの作る
def ClosureObject(x):
self = dict()
self['x'] = x
def getX():
return self['x']
self['getX'] = getX
def setX(x):
self['x'] = x
self['setX'] = setX
return self
obj = ClosureObject(10)
assert obj['getX']() == 10
obj['setX'](20)
assert obj['getX']() == 20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment