秒懂裝飾器(帶傳參和返回值)

2021-08-21 17:54:14 字數 977 閱讀 2390

#-*- coding:utf-8 -*-

import time

'''傳入的引數為func,即函式名(函式的位址),不是func(),若是func()即把函式的執行結果傳入了timer'''

deftimer

(func):

#將函式名傳遞給另乙個函式作為引數,稱之為高階函式

defdeco

(*args,**kargs):

#在函式內定義乙個函式,稱之為函式的巢狀

start_time=time.time()

res=func(*args,**kargs)

stop_time=time.time()

print("the cost time is %s" %(stop_time-start_time))

return res

return deco

@timer #相當於test1=timer(test1),即把deco的位址給了test1

deftest1

(): time.sleep(1)

print("in the test1")

@timer

deftest2

(name,age):

time.sleep(2)

print("in the test2",name,age)

return

"test2 is running"

test1()

print(test2("kid",20))

in the test1

the cost time is 1.0076828002929688

in the test2 kid 20

the cost time is 2.0213704109191895

test2 is running

python 帶返回值的裝飾器

基本裝飾器沒有引數沒有返回值 當功能函式有返回值的情況下 解決返回值的問題 import time 裝飾器函式 def cont time func 統計時間的裝飾器 def inner start time time.time print 計時開始。func end time time.time ...

儲存過程分頁,帶參,返回值

create procedure dbo nb arrearagelistbycar pageindex int,第幾頁 pagesize int,分頁大小 carnumber nvarchar 6 車牌號 rstatus int output,返回標識號 0 成功 rcode int output...

關於函式傳參及返回值

很多剛學指標的人多會遇到這樣的情況,就是你給乙個函式傳遞乙個指標變數,在這個函式中為這個指標申請空 間,賦值,可是等函式結束後這個指標還是乙個空指標,對其的操作編譯器會報段錯誤或乾脆給你乙個莫名其妙的值,這是乙個隨機數。如 include include int my fun int p int m...