Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@lene
Last active December 21, 2018 09:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lene/d1189ede6a2df20619d97ad39b1200d7 to your computer and use it in GitHub Desktop.
Save lene/d1189ede6a2df20619d97ad39b1200d7 to your computer and use it in GitHub Desktop.
Python decorator with argument(s)
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