Skip to content

Instantly share code, notes, and snippets.

@cardoso243
Forked from mchung/callback.py
Created February 14, 2020 00:24
Show Gist options
  • Select an option

  • Save cardoso243/ee6e8581c4f22fc9ad466050d43d32db to your computer and use it in GitHub Desktop.

Select an option

Save cardoso243/ee6e8581c4f22fc9ad466050d43d32db to your computer and use it in GitHub Desktop.
Example of callbacks with Python
# Example of using callbacks with Python
#
# To run this code
# 1. Copy the content into a file called `callback.py`
# 2. Open Terminal and type: `python /path/to/callback.py`
# 3. Enter
def add(numbers, callback):
results = []
for i in numbers:
results.append(callback(i))
return results
def add2(number):
return number + 2
def mul2(number):
return number * 2
print add([1,2,3,4], add2) #=> [3, 4, 5, 6]
print add([1,2,3,4], mul2) #=> [2, 4, 6, 8]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment