Skip to content

Instantly share code, notes, and snippets.

@kgriffs
Created June 7, 2013 15:11
Show Gist options
  • Save kgriffs/5729976 to your computer and use it in GitHub Desktop.
Save kgriffs/5729976 to your computer and use it in GitHub Desktop.
Example of dynamically decorating a method in Python
import functools
class Scooby(object):
def mood(self):
return 'frightended'
def scooby_snack(func):
@functools.wraps(func)
def brave(self):
return 'not ' + func(self)
return brave
scooby = Scooby()
print('Scooby: <' + scooby.mood() + '>')
print('Fred: Would you do it for a Scooby Snack?')
Scooby.mood = scooby_snack(Scooby.mood)
print('Scooby: <' + scooby.mood() + '>')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment