Skip to content

Instantly share code, notes, and snippets.

@podhmo
Created November 8, 2015 01:46
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 podhmo/0e14fa0fbf1a8e722fe2 to your computer and use it in GitHub Desktop.
Save podhmo/0e14fa0fbf1a8e722fe2 to your computer and use it in GitHub Desktop.
# -*- 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