Last active
December 21, 2018 09:20
-
-
Save lene/d1189ede6a2df20619d97ad39b1200d7 to your computer and use it in GitHub Desktop.
Python decorator with argument(s)
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 | |
def decorator_with_argument(argument=None): | |
def actual_decorator(func): | |
@wraps(func) | |
def func_wrapper(*args, **kwargs): | |
return do_stuff(func(*args, **kwargs), argument) | |
return func_wrapper | |
return actual_decorator | |
@decorator_with_argument('blah') | |
def example_1(): | |
return 1 # returns do_stuff(1, 'blah') | |
# note the brackets! | |
@decorator_with_argument() | |
def example_2(): | |
return 1 # returns do_stuff(1, None) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment