Created
July 26, 2017 04:32
-
-
Save vrootic/7d1e0902683bff9d05aa9bf5edd6aac1 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
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