Created
September 19, 2019 11:40
-
-
Save NimzyMaina/ca127dbafc6fb4cb1f35dffd7339d17e to your computer and use it in GitHub Desktop.
How to create decorators 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
# A decorator is just a function that gets called before another function | |
import functools # function tools | |
def my_decorator(f): | |
@functools.wraps(f) | |
def function_that_runs_f(): | |
print("Hello!") | |
f() | |
print("After!") | |
return function_that_runs_f | |
@my_decorator | |
def my_function(): | |
print("I'm in the function.") | |
my_function() | |
### | |
def my_decorator(f): | |
@functools.wraps(f) | |
def function_that_runs_f(*args, **kwargs): | |
print("Hello!") | |
f(*args, **kwargs) | |
print("After!") | |
return function_that_runs_f | |
@my_decorator | |
def my_function(arg1, arg2): | |
print(arg1 + arg2) | |
my_function(56, 89) | |
### | |
def decorator_arguments(number): | |
def my_decorator(f): | |
@functools.wraps(f) | |
def function_that_runs_f(*args, **kwargs): | |
print("Hello!") | |
if number == 56: | |
print("Not running!") | |
else: | |
f(*args, **kwargs) | |
print("After") | |
return function_that_runs_f | |
return my_decorator | |
@decorator_arguments(56) | |
def my_function(): | |
print("Hello!") | |
my_function() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment