Skip to content

Instantly share code, notes, and snippets.

@astex
Created May 5, 2014 16:19
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 astex/d4a88e7d2ef9e1f9ccb8 to your computer and use it in GitHub Desktop.
Save astex/d4a88e7d2ef9e1f9ccb8 to your computer and use it in GitHub Desktop.
Python methodize decorator
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