Created
March 14, 2017 20:01
-
-
Save Microserf/e5a5be35ea845a1dcd7503773ea05864 to your computer and use it in GitHub Desktop.
A converter for turning a function decorator into a method 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
import functools | |
def make_method_decorator(decorator): | |
# This function returns a version of @decorator fit for wrapping a method | |
def method_wrapper(method): | |
@functools.wraps(method) | |
def inner_method(self, *args, **kwargs): | |
@decorator | |
def inner_func(*inner_args, **inner_kwargs): | |
return method(self, *inner_args, **inner_kwargs) | |
return inner_func(*args, **kwargs) | |
return inner_method | |
return method_wrapper |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment