Skip to content

Instantly share code, notes, and snippets.

@maple42
Created October 13, 2014 07:54
Show Gist options
  • Save maple42/5b0da2742dacdc095358 to your computer and use it in GitHub Desktop.
Save maple42/5b0da2742dacdc095358 to your computer and use it in GitHub Desktop.
decorator of log
#!/usr/bin/env python
#_*_ conding:utf8 _*_
def log(string):
if isinstance(string,str):
def decorator(func):
def wrapper(*args,**kw):
print '%s %s' %(string,func.__name__)
return func(*args,**kw)
return wrapper
return decorator
else:
def wrapper(*args,**kw):
print '%s' %string.__name__
return string(*args,**kw)
return wrapper
@log
def f1():
pass
@log('excute')
def f2():
pass
f1()
f2()
"""
--------------------------------------------------
Result:
[root@njrd117 tools]# ./decorator.py
f1
excute f2
--------------------------------------------------
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment