看代碼理解python裝飾器

之前使用的,查看函數運行時間的方法:app

#encoding:utf-8
import time

def trainModel():
    print("training...")
    time.sleep(2)

def testModel():
    print("testing...")
    time.sleep(1)

if __name__ == "__main__":
    start = time.time()
    trainModel() #訓練模型
    end = time.time()
    print("cost time:",end-start)

    start = time.time()
    testModel() #測試模型
    end = time.time()
    print("cost time:",end-start)
    """
    training...
    cost time: 2.002230644226074
    testing...
    cost time: 1.0011751651763916
    """

或者是函數

#encoding:utf-8
import time

def trainModel():
    start = time.time() #寫入函數內部
    print("training...")
    end = time.time()
    print("cost time:",end-start)

def testModel():
    start = time.time()
    print("testing...")
    end = time.time()
    print("cost time:",end-start)

if __name__ == "__main__":
    trainModel() #訓練模型
    testModel() #測試模型
    """
    training...
    cost time: 1.1682510375976562e-05
    testing...
    cost time: 2.1457672119140625e-06
    """

使用裝飾器後發現真好用!!!測試

裝飾器(裝飾器爲函數,被裝飾的也是函數)
#encoding:utf-8
import time
def costTime(func):
    def wrapper(*args, **kwargs):
	start = time.time()
	f = func(*args, **kwargs)
	end = time.time()
	print("cost time:",end-start)
	return f
    return wrapper

#至關於 costTime(trainModel())
#將trainModel拿到costTime中的wrapper中運行
#便可在trainModel()先後添加裝飾代碼

@costTime
def trainModel():
    print("training...")
@costTime
def testModel():
    print("testing...")

if __name__ == "__main__":
    trainModel() #訓練模型
    testModel() #測試模型
    """
    training...
    cost time: 1.1682510375976562e-05
    testing...
    cost time: 2.1457672119140625e-06
    """

#裝飾器(裝飾器爲函數,被裝飾的也是函數,傳參數)
#encoding:utf-8
import time

def costTime(flag):
    def wrapper(func):
	def inner_wrapper(*args, **kwargs):
	    start = time.time()
	    f = func(*args, **kwargs)
	    end = time.time()
	    if flag == "True":
		print("cost time:",end-start)
	    return f
	return inner_wrapper
    return wrapper

@costTime(flag = "True")
def trainModel():
    print("training...")
@costTime(flag = "False")
def testModel():
    print("testing...")

if __name__ == "__main__":
    trainModel() #訓練模型
    testModel() #測試模型
    """
    training...
    cost time: 1.2159347534179688e-05
    testing...
    """

#裝飾器(裝飾器爲函數,被裝飾的是類)
#encoding:utf-8
import time

def costTime(func):
    def wrapper(*args, **kwargs):
	start = time.time()
	f = func(*args, **kwargs)
	end = time.time()
	print("cost time:",end-start)
	return f
    return wrapper

@costTime
class trainModel:
    def __init__(self):
	print("training...")
@costTime
class testModel:
    def __init__(self):
	print("testing...")

if __name__ == "__main__":
    trainModel() #訓練模型
    testModel() #測試模型
    """
    training...
    cost time: 1.2159347534179688e-05
    testing...
    cost time: 2.86102294921875e-06
    """

#裝飾器(裝飾器爲函數,被裝飾的是類,傳參數)
#encoding:utf-8
import time

def costTime(flag):
    def wrapper(func):
	def inner_wrapper(*args, **kwargs):
	    start = time.time()
	    f = func(*args, **kwargs)
	    end = time.time()
	    if flag == "True":
		print("cost time:",end-start)
	    return f
	return inner_wrapper
    return wrapper

@costTime(flag = "True")
class trainModel:
    def __init__(self):
	print("training...")
@costTime(flag = "False")
class testModel:
    def __init__(self):
	print("testing...")

if __name__ == "__main__":
    trainModel() #訓練模型
    testModel() #測試模型
	"""
    training...
    cost time: 1.1920928955078125e-05
    testing...
    """

#裝飾器(裝飾器爲類,被裝飾的是函數)
#encoding:utf-8
import time

class costTime():
    def __init__(self,func):
	self.fun = func
	#重載__call__方法是就須要接受一個函數並返回一個函數
	def __call__(self,*args, **kwargs):
	    start = time.time()
	    f = self.fun(*args, **kwargs)
	    end = time.time()
	    print("cost time:",end-start)
	    return f

@costTime
def trainModel():
    print("training...")

@costTime
def testModel():
    print("testing...")

if __name__ == "__main__":
    trainModel() #訓練模型
    testModel() #測試模型
    """
    training...
    cost time: 1.1682510375976562e-05
    testing...
    cost time: 2.1457672119140625e-06
    """

#裝飾器(裝飾器爲類,被裝飾的也是函數,傳參數)
#encoding:utf-8
import time

class costTime:
    def __init__(self,flag):
	self.flag = flag
    def __call__(self,func):
	def wrapper(*args, **kwargs): 
	    start = time.time()
	    f = func(*args, **kwargs)
	    end = time.time()
	    if self.flag == "True":
		print("cost time:",end-start)
	    return f
        return wrapper

@costTime(flag = "True")
def trainModel():
    print("training...")
@costTime(flag = "False")
def testModel():
    print("testing...")

if __name__ == "__main__":
    trainModel() #訓練模型
    testModel() #測試模型
    """
    training...
    cost time: 1.2874603271484375e-05
    testing...
    """

#裝飾器(裝飾器爲類,被裝飾的也是類)
#encoding:utf-8
import time

class costTime():
    def __init__(self,func):
	self.fun = func
	#重載__call__方法是就須要接受一個函數並返回一個函數
	def __call__(self,*args, **kwargs):
	    start = time.time()
	    f = self.fun(*args, **kwargs)
	    end = time.time()
	    print("cost time:",end-start)
	    return f

@costTime
class trainModel:
    def __init__(self):
	print("training...")
@costTime
class testModel:
    def __init__(self):
	print("testing...")

if __name__ == "__main__":
    trainModel() #訓練模型
    testModel() #測試模型
    """
    training...
    cost time: 1.2159347534179688e-05
    testing...
    cost time: 2.86102294921875e-06
    """
	
#裝飾器(裝飾器爲函數,被裝飾的是類,傳參數)
#encoding:utf-8
import time

class costTime:
    def __init__(self,flag):
	self.flag = flag
    def __call__(self,func):
	def wrapper(*args, **kwargs): 
	    start = time.time()
	    f = func(*args, **kwargs)
	    end = time.time()
	    if self.flag == "True":
	        print("cost time:",end-start)
	    return f
	return wrapper

@costTime(flag = "True")
class trainModel:
    def __init__(self):
	print("training...")

@costTime(flag = "False")
class testModel:
    def __init__(self):
	print("testing...")

if __name__ == "__main__":
    trainModel() #訓練模型
    testModel() #測試模型
    """
    training...
    cost time: 1.1920928955078125e-05
    testing...
    """
相關文章
相關標籤/搜索