黑猴子的家 python 裝飾器

2021-09-11 16:44:08 字數 704 閱讀 1517

裝飾器本質是函式,(裝飾其他函式)就是為其他函式新增附加功能

(1)不能修改被裝飾的函式的源**

(2)不能修改被裝飾的函式的呼叫方式

(3)裝飾器對它被裝飾的韓式是完全透明的

(1)函式即「變數」

(2)高階函式

(3)巢狀函式

code

import time

#裝飾器

def timmer(func):

def warpper(*args,**kwargs):

start_time=time.time()

func()

stop_time=time.time()

print('the func run time is %s' %(stop_time-start_time))

return warpper

@timmer

def t1():

time.sleep(3)

print('in the test1')

t1()

列印

in the test1

the func run time is 3.002000331878662

黑猴子的家 python 裝飾器之高階函式

1 把乙個函式名當做實參傳給另外乙個函式,在不修改被裝飾的函式源 的情況下 為其新增功能 2 返回值中包含函式名,不修改函式的呼叫方式code 情況1 import time def bar time.sleep 3 print in the bar def t1 func print func s...

黑猴子的家 python 裝飾器實現的迭代過程

code 高階函式 迭代裝飾器一 def deco func start time time.time func stop time time.time print the func run time is s stop time start time def deco1 func star tim...

黑猴子的家 Python 簡介

總的來說,程式語言各有千秋。c語言是可以用來編寫作業系統的貼近硬體的語言,所以,c語言適合開發那些追求執行速度 充分發揮硬體效能的程式。而python是用來編寫應用程式的高階程式語言。當你用一種語言開始作真正的軟體開發時,你除了編寫 外,還需要很多基本的已經寫好的現成的東西,來幫助你加快開發進度。比...