Skip to content

Instantly share code, notes, and snippets.

@sidchilling
Created April 29, 2013 06:36
Show Gist options
  • Select an option

  • Save sidchilling/5480024 to your computer and use it in GitHub Desktop.

Select an option

Save sidchilling/5480024 to your computer and use it in GitHub Desktop.
Script to show how to pass a function as a parameter to another function
# If a function can return a function, then a function can also take a function
# as an argument
# Defining a function which we will pass as an argument to another function
def scream(word = 'yes'):
return '%s!' %(word.upper())
# Function taking a function as argument
def do_something_before(func):
# Do something before calling the passed function
print "I do something before then I call the function you gave me"
# Now call the passed function
print func()
do_something_before(scream)
# Outputs:
# I do something before then I call the function you gave me
# YES!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment