Last active
January 1, 2025 13:03
-
-
Save Sllayan/45d8dc7d8b47a2a5c72447907e0b0c07 to your computer and use it in GitHub Desktop.
Simple example of decorator in Python
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
| # Decorator function | |
| def my_decorator(func): | |
| def wrapper(): | |
| print('Anything that needs to be done before the function is called.') | |
| func() | |
| print('Anything that needs to be done after the function is called.') | |
| return wrapper | |
| # First usecase | |
| def say_hello(): | |
| print('hello') | |
| say_it = my_decorator(say_hello) | |
| say_it() | |
| # Second usecase | |
| @my_decorator | |
| def say_hello(): | |
| print('hello') | |
| say_hello() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment