Skip to content

Instantly share code, notes, and snippets.

@odra
Created February 25, 2016 17:16
Show Gist options
  • Save odra/973201ea998374689491 to your computer and use it in GitHub Desktop.
Save odra/973201ea998374689491 to your computer and use it in GitHub Desktop.
add some function in a class instance dictionary within a decorator
# -*- encoding: utf-8 -*-
from functools import wraps
class C(object):
def __init__(self):
self.methods = {}
def method(self, name):
def decorator(fn):
@wraps(fn)
def wrapper(*args, **kwargs):
self.methods[name] = fn
return fn(*args, **kwargs)
return wrapper
return decorator
c = C()
@c.method('hello')
def my():
print 'hello'
my()
print c.methods # prints {'hello': my}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment