Created
April 29, 2013 06:36
-
-
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
This file contains hidden or 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
| # 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