当前位置:网站首页>Decorator functions and the use of class decorators
Decorator functions and the use of class decorators
2022-07-27 06:39:00 【Leoon123】
One 、 Decorator
Decorator : Decorating functions and classes , effect : Extend and enhance functions and classes
Two 、 The classification of decorators
Two categories: : Decorator functions and decorator classes
3、 ... and 、 Decorator function definition and Application
function : The smallest unit of encapsulated code , Provide code reusability
Decorator functions take advantage of some features of functions :
1、 The function can be used as an argument
3、 Functions can be variables
2、 Function can also return function
Decorator function essence : Call the built-in method of decoration function
Four 、 Use of decorator functions
import time
def display_time(fun):
def wrapper():
t1 = time.time()
resutl =fun()
t2 = time.time()
print(" The total time is {:.4} s".format(t2 - t1))
return resutl
return wrapper
def is_price(num):
if num < 2:
return False
elif num == 2:
return True
else:
for i in range(2, num):
if num % i == 0:
return False
return True
'''
Compared with the front , More return value ,
Need to be in display_time Add... To the function fun() Return results
'''
@display_time
def count_prime_number():
count=0
for i in range(2, 1000):
if is_price(i):
count=count+1
return count
counts=count_prime_number()
print(counts)
5、 ... and 、 The use of class decorators
import time
# Define decoration class ‐‐‐‐‐》 Essential class effect : Original function + Extend the functionality
class A:
# Original function self._func()
def __init__(self, func):
self._func = func
# Extend the functionality
def __call__(self, *args, **kwargs):
"""
__call__ function : Instantiate objects ()
:return:
"""
start = time.time()
# Original function
result=self._func(*args, **kwargs) # Original function
end = time.time()
print(" The total time is {:.4} s".format(end - start))
return result
@A
def count_prime_number(maxnumber):
time.sleep(1)
sum=0
for i in range(2, maxnumber):
sum=sum+i
return sum
sums=count_prime_number(10000)
print(sums)
边栏推荐
猜你喜欢

Shell programming specifications and variables

Knowledge supplement of multithreading

互联网简单协议概括

数组及下标索引

Lamp -- source code compilation and installation

正则表达式

DHCP原理与配置

Li Kou daily question leetcode 513. find the value in the lower left corner of the tree

DNS domain name resolution service

Source code compilation and installation lamp and discuz Forum
随机推荐
Summary of test basis
Solve the problems of CONDA install stop and interruption
Li Kou daily question (linked list simulation)
数据库命令
DHCP principle and configuration
Remote access and control
Three ways to get RPM packages using yum
网络故障排查:Ping和Tracert命令
源码编译安装LNMP和DISCUZ论坛
Ram of IP core
shell--自定义变量与赋值
How to write yaml file in a standard way
shell--循环语句(for、while、until)
Robot navigation
如何规范式编写yaml文件
shell函数
基于Apache下ab工具进行网站压力性能测试
源码编译安装LAMP和DISCUZ论坛
Geonode GeoServer win10 installation tutorial (personal test)
Briefly remember the top ten orders