Skip to content

Commit faa02c3

Browse files
committed
Push Code
1 parent 89fe72e commit faa02c3

File tree

6 files changed

+166
-0
lines changed

6 files changed

+166
-0
lines changed

Chapter 6/6_12_1.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""
2+
装饰器代码示例
3+
"""
4+
import time
5+
6+
7+
def 计时(配料="珍珠"):
8+
def decorator(func):
9+
def inner():
10+
print("加入配料:%s" % 配料 )
11+
start = time.time()
12+
func()
13+
end = time.time()
14+
print("耗时", end - start)
15+
return func
16+
return inner
17+
18+
return decorator
19+
20+
21+
@计时(配料='波霸')
22+
def 波霸奶茶():
23+
time.sleep(1)
24+
print("制作一杯波霸奶茶")
25+
26+
27+
@计时(配料='椰果')
28+
def 四季奶绿():
29+
time.sleep(2)
30+
print("制作一杯四季奶绿")
31+
32+
33+
if __name__ == '__main__':
34+
波霸奶茶()
35+
print()
36+
四季奶绿()

Chapter 6/6_12_2.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""
2+
多个装饰器装饰函数的执行顺序示例
3+
"""
4+
5+
6+
def func_a(func):
7+
def decorator():
8+
func()
9+
print("Call func_a")
10+
11+
return decorator
12+
13+
14+
def func_b(func):
15+
def decorator():
16+
func()
17+
print("Call func_b")
18+
19+
return decorator
20+
21+
22+
def func_c(func):
23+
def decorator():
24+
func()
25+
print("Call func_c")
26+
27+
return decorator
28+
29+
30+
@func_a
31+
@func_b
32+
@func_c
33+
def func_t():
34+
pass
35+
36+
37+
if __name__ == '__main__':
38+
func_t()

Chapter 6/6_12_3.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"""
2+
函数装饰函数
3+
"""
4+
5+
6+
def func_func(func):
7+
def decorator(a, b):
8+
print("函数修饰的函数名:", func.__name__)
9+
result = func(a, b)
10+
return result
11+
12+
return decorator
13+
14+
15+
@func_func
16+
def func_add(a, b):
17+
return a + b
18+
19+
20+
if __name__ == '__main__':
21+
print(func_add(1, 2))

Chapter 6/6_12_4.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""
2+
函数装饰类代码示例
3+
"""
4+
5+
6+
def func_class(cls):
7+
def decorator(name):
8+
print("函数修饰的类:", cls.__name__, name)
9+
return cls(name)
10+
11+
return decorator
12+
13+
14+
@func_class
15+
class A:
16+
def __init__(self, n):
17+
self.n = n
18+
19+
def show(self):
20+
print("a = " + self.n)
21+
22+
23+
a = A('123')
24+
a.show()

Chapter 6/6_12_5.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"""
2+
类装饰函数代码示例
3+
"""
4+
5+
6+
class class_func:
7+
def __init__(self, _func):
8+
self._func = _func
9+
10+
def __call__(self, name):
11+
print("类装饰的方法名:", self._func.__name__, name)
12+
return self._func(name)
13+
14+
15+
@class_func
16+
def func(a):
17+
return a
18+
19+
20+
if __name__ == '__main__':
21+
print(func('123'))

Chapter 6/6_12_6.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""
2+
类装饰类代码示例
3+
"""
4+
5+
6+
class class_class:
7+
def __init__(self, _cls):
8+
self._cls = _cls
9+
10+
def __call__(self, name):
11+
print("类装饰的类的类名:", self._cls.__name__, name)
12+
return self._cls(name)
13+
14+
15+
@class_class
16+
class A:
17+
def __init__(self, a):
18+
self.a = a
19+
20+
def show(self):
21+
print('self.a = ', self.a)
22+
23+
24+
if __name__ == '__main__':
25+
a = A('123')
26+
a.show()

0 commit comments

Comments
 (0)