Skip to content

Instantly share code, notes, and snippets.

@vrootic
Created July 26, 2017 04:32
Show Gist options
  • Save vrootic/7d1e0902683bff9d05aa9bf5edd6aac1 to your computer and use it in GitHub Desktop.
Save vrootic/7d1e0902683bff9d05aa9bf5edd6aac1 to your computer and use it in GitHub Desktop.
def apply_async(func, args, *, callback):
result = func(*args)
callback(result)
def add(x, y):
return x + y
# Using closure
def make_handler():
sequence = 0
def handler(result):
nonlocal sequence
sequence += 1
print('[{}] Got: {}'.format(sequence, result))
return handler
handler = make_handler()
apply_async(add, (2, 3), callback=handler) # [1] Got: 5
apply_async(add, ('hello', 'world'), callback=handler) # [2] Got: helloworld
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment