Skip to content

Instantly share code, notes, and snippets.

@mietek
Created August 18, 2014 10:42
Show Gist options
  • Save mietek/e9e05d93e2517073017b to your computer and use it in GitHub Desktop.
Save mietek/e9e05d93e2517073017b to your computer and use it in GitHub Desktop.
Laziness in Python 2.x
#!/usr/bin/env python
def force(thunk):
return thunk()
class delay:
def __init__(self, func):
self.func = func
def remember():
result = self.func()
def recall():
return result
self.thunk['func'] = recall
return result
self.thunk = {}
self.thunk['func'] = remember
def __call__(self):
return self.thunk['func']()
def hello():
print "hello"
thunk = delay(hello)
force(thunk)
force(thunk)
force(thunk)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment