forked from ylzon/python-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
EvanBell
committed
Nov 1, 2017
1 parent
f939e1e
commit d0d30b8
Showing
4 changed files
with
57 additions
and
24 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,56 @@ | ||
#!/usr/bin/env python | ||
# -*- coding:utf-8 -*- | ||
|
||
|
||
|
||
# 多层装饰器 | ||
def outer_fist(func): | ||
def inner(*args, **kwargs): | ||
print("第一层开始") | ||
# 装饰器函数 | ||
import time | ||
|
||
|
||
def decorator_2(func): | ||
""" | ||
最外层装饰器 | ||
:param func: | ||
:return: | ||
""" | ||
def warp(*args, **kwargs): | ||
print("最外层装饰器".center(20, "=")) | ||
ret = func(*args, **kwargs) | ||
print("第一层结束") | ||
print("最外层装饰器".center(20, "=")) | ||
return ret | ||
|
||
return inner | ||
return warp | ||
|
||
|
||
def outer(func): | ||
def inner(*args, **kwargs): | ||
print("开始") | ||
ret = func(*args, **kwargs) | ||
print("结束") | ||
return ret | ||
def decorator_1(auth_type): | ||
""" | ||
内层装饰器,计算函数运行时间 | ||
:param auth_type: | ||
:return: | ||
""" | ||
# print("auth type:", auth_type) | ||
|
||
def out_warp(func): | ||
# print("func:", func) | ||
|
||
def warp(*args, **kwargs): | ||
# print(*args, **kwargs) | ||
start_time = time.time() | ||
ret = func(*args, **kwargs) | ||
end_time = time.time() | ||
print("函数执行时间:", end_time - start_time) | ||
return ret | ||
|
||
return warp | ||
|
||
return inner | ||
return out_warp | ||
|
||
|
||
@outer_fist | ||
@outer | ||
def main(a1, a2): | ||
print("内容") | ||
return a1 + a2 | ||
@decorator_2 | ||
@decorator_1(auth_type="local") | ||
def home(*args, **kwargs): | ||
print("this is home,sleeping...") | ||
time.sleep(3) | ||
return args[0] + args[1] | ||
|
||
|
||
ret = main(1, 2) | ||
print(ret) | ||
i = home(1, 2) | ||
print("最后返回结果:", i) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#!/usr/bin/env python | ||
# -*- coding:utf-8 -*- | ||
|
||
|
||
|