Created
May 5, 2014 16:19
-
-
Save astex/d4a88e7d2ef9e1f9ccb8 to your computer and use it in GitHub Desktop.
Python methodize decorator
This file contains 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 methodize(dec): | |
"""Decorates a function decorator so it can wrap methods. | |
Usage: | |
``` | |
@methodize(decorator) | |
function(self, *args, **kargs): | |
#... | |
``` | |
""" | |
def adapt(f): | |
@wraps(f) | |
def decorated(self, *args, **kargs): | |
@dec | |
def g(*a, **k): | |
return f(self, *a, **k) | |
return g(*args, **kargs) | |
return decorated | |
return adapt |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment