首页 > 其他 > 详细

装饰器&装饰类

时间:2020-05-30 19:24:32      阅读:46      评论:0      收藏:0      [点我收藏+]

装饰器例子:

技术分享图片
 1 import time
 2 
 3 def math_type(type = ‘‘):
 4     def decorator_time(math_closure):
 5         def new_func(*args, **argss):
 6             print(type)
 7             start = time.time()
 8             print(time.time())
 9             result = math_closure(*args, **argss)#返回math_method
10             print(result(type))#调用math_method
11             end = time.time()
12             print(end)
13             print(end-start)
14             return result
15         return new_func
16     return decorator_time
17 
18 @ math_type(add)
19 def math_closure_return(*args, **argss):
20     def math_method(x):
21         if x==add:
22             print(type(args))
23             sum = 0
24             for i in args:
25                 sum += i
26             for key in argss.keys():
27                 sum += argss[key]
28             return sum
29         if x==minu:
30             sum = 0
31             for i in args:
32                 sum -= i
33             for key in argss.keys():
34                 sum -= argss[key]
35             return sum
36     return math_method
37 fun = math_closure_return(12,3,a=5,b=3)#返回math_method
38 print(fun(minu))
39 #带参装饰器
40 def math_type(type = ‘‘):
41     def deco_time(math_closure):
42         def new_func(*args, **argss):
43             print(type)
44             start = time.time()
45             print(time.time())
46             print(start)
47             return_math_method = math_closure(*args, **argss)#返回的是math_method函数
48             return_math_method(type)#调用math_method
49             print(end)
50             end = time.time()
51             print(end)
52             print(end-start)
53             return return_math_method
54         return new_func
55     return deco_time
56 
57 @ math_type(add)
58 def math_closure_print(*args, **argss):
59     time.sleep(1)
60     def math_method(x):
61         if x==add:
62             print(type(args))
63             sum = 0
64             for i in args:
65                 sum += i
66             for key in argss.keys():
67                 sum += argss[key]
68             print(sum)
69         if x==minu:
70             sum = 0
71             for i in args:
72                 sum -= i
73             for key in argss.keys():
74                 sum -= argss[key]
75             print(sum)
76     return math_method
77 fun = math_closure_print(12,3,a=5,b=3)
78 #fun(‘minu‘)#有此句则多调用一次math_method(‘minu‘)
View Code

装饰类例子:

技术分享图片
 1 def decorate_class(SomeClass):#函数
 2     class NewClass():
 3         def __init__(self,age):
 4             self.total_display = 0#新增对象属性
 5             self.wrapped       = SomeClass(age)#新增对象属性,属性名随意,记录原类生成的对象
 6         def display(self):
 7             self.total_display += 1
 8             print(ftotal display:{self.total_display})
 9             self.wrapped.display()
10     return NewClass
11 
12 @decorate_class
13 class Bird:
14     def __init__(self,age):
15         self.age = age
16     def display(self):
17         print(fMy age is:{self.age})
18 eagle_lord = Bird(5)
19 for i in range(3):
20     eagle_lord.display()
View Code

 

装饰器&装饰类

原文:https://www.cnblogs.com/gloria5/p/12993746.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!