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
| def my_shiny_new_decorator(a_function_to_decorate): | |
| # Inside, the decorator defines a function on the fly: the wrapper. | |
| # This function is going to be wrapped around the original function | |
| # so it can execute code before and after it. | |
| def the_wrapper_around_the_original_function(): | |
| # Put here the code you want to be executed BEFORE the original | |
| # function is called | |
| print("Before the function runs") |
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
| def doSomethingBefore(func): | |
| print("I do something before then I call the function you gave me") | |
| print(func()) | |
| doSomethingBefore(scream) | |
| # Outputs: | |
| # I do something before then I call the function you gave me | |
| # Yes! |
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
| # Get the function and assign it to a variable | |
| talk = getTalk() | |
| # You can see that `talk` is here a function object: | |
| print(talk) | |
| # Outputs : <function shout at 0xc1d2859b> | |
| # The object is the one returned by the function: | |
| print(talk()) | |
| # Outputs: Yes! |
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
| def getTalk(kind='shout'): | |
| # We define functions on the fly | |
| def shout(word='yes'): | |
| return word.capitalize() + '!' | |
| def whisper(word='yes'): | |
| return word.lower() + '...' | |
| # Then we return one of them | |
| if kind == 'shout': |
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
| try: | |
| print(whisper()) | |
| except NameError as e: | |
| print(e) | |
| # Outputs: "name 'whisper' is not defined" |
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
| def talk(): | |
| # Defining a function on the fly in `talk` ... | |
| def whisper(word='yes'): | |
| return word.lower() + '...' | |
| # ... and using it right away! | |
| print(whisper()) | |
| talk() | |
| # Outputs: "yes..." |
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
| # deleting the shout() object | |
| del shout | |
| try: | |
| # trying to access the deleted shout() object | |
| print(shout()) | |
| except NameError as e: | |
| print(e) | |
| # Outputs: "name 'shout' is not defined" |
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
| print(scream()) | |
| # Outputs: 'Yes!' |
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
| scream = shout |
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
| def shout(word='yes'): | |
| return word.capitalize() + '!' | |
| print(shout()) | |
| # Outputs : 'Yes!' |