Created
November 8, 2015 01:46
-
-
Save podhmo/0e14fa0fbf1a8e722fe2 to your computer and use it in GitHub Desktop.
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
# -*- coding:utf-8 -*- | |
def attach(f): | |
def _attach(*args, **kwargs): | |
print("attached: {}, {}".format(f, args)) | |
return f(*args, **kwargs) | |
return _attach | |
def attach_with_args(message): | |
def _attach_with_args(f): | |
def _attach(*args, **kwargs): | |
print(message.format(f, args)) | |
return f(*args, **kwargs) | |
return _attach | |
return _attach_with_args | |
def attach_with_args_with_kindness(f=None, message="attached: {}, {}"): | |
"""@attach() and @attach also ok""" | |
def call(f): | |
def _attach(*args, **kwargs): | |
print(message.format(f, args)) | |
return f(*args, **kwargs) | |
return _attach | |
if f is not None: | |
return call(f) | |
else: | |
return call | |
if __name__ == "__main__": | |
@attach | |
def f(x): | |
return x * x | |
f(10) | |
@attach_with_args(message="new attached: {}, {}") | |
def f(x): | |
return x * x | |
f(100) | |
@attach_with_args_with_kindness | |
def f(x): | |
return x * x | |
f(1000) | |
@attach_with_args_with_kindness(message="hai: {}, {}") | |
def f(x): | |
return x * x | |
f(1000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment