Skip to content

Commit

Permalink
add base demo16
Browse files Browse the repository at this point in the history
  • Loading branch information
EvanBell committed Nov 1, 2017
1 parent f939e1e commit d0d30b8
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 24 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

## 目录

* [Python基础语法练习](https://github.com/mgss/python-demo/blob/master/docs/basic.md) (15)

* [Python基础语法练习](https://github.com/mgss/python-demo/blob/master/docs/basic.md) (16)
* [Python基本算法练习](https://github.com/mgss/python-demo/blob/master/docs/algo.md) (2)
* Python面向对象
* python网络编程
Expand Down
8 changes: 7 additions & 1 deletion docs/basic.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,10 @@ PS:字典中的value只能是字符串或列表

* 命令行实现用户登录、注册操作,并以文本文件保存用户名密码数据

参考Demo:[链接](https://github.com/mgss/python-demo/blob/master/example/basic/demo15/user.py)
参考Demo:[链接](https://github.com/mgss/python-demo/blob/master/example/basic/demo15/user.py)

## Demo-16

* 实现多层装饰器及任意传参

参考Demo:[链接](https://github.com/mgss/python-demo/blob/master/example/basic/demo16.py)
65 changes: 43 additions & 22 deletions example/basic/demo16.py
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)
5 changes: 5 additions & 0 deletions example/basic/demo17.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env python
# -*- coding:utf-8 -*-



0 comments on commit d0d30b8

Please sign in to comment.