Created
August 31, 2021 04:12
-
-
Save FerdinaKusumah/540fdbd9262c2089b21f5e0310afd630 to your computer and use it in GitHub Desktop.
Python simple decorator
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
from functools import wraps | |
# Defining our custom decorator | |
def my_decorator(function): | |
@wraps(function) | |
def wrapper(a, b, c): | |
print("wrapper running!") | |
a += 1 | |
b += 2 | |
c += 3 | |
return function(a, b, c) | |
return wrapper | |
# Using it to decorate a function | |
@my_decorator | |
def my_function(a, b, c): | |
print("my_function running!") | |
print(a, b, c) | |
my_function(a=1, b=2, c=3) | |
# wrapper running! | |
# my_function running! | |
# 2 4 6 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment