Created
October 13, 2014 07:54
-
-
Save maple42/5b0da2742dacdc095358 to your computer and use it in GitHub Desktop.
decorator of log
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
#!/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