Created
February 25, 2016 17:16
-
-
Save odra/973201ea998374689491 to your computer and use it in GitHub Desktop.
add some function in a class instance dictionary within a 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
# -*- 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