裝飾器是一個返回函數的高階函數。python
def logger(func): def wrapper(*args, **kw): print 'do {}'.format(func.__name__) func(*args, **kw) print 'finish' return wrapper @logger def add(x,y): print '{} + {} = {}'.format(x,y,x+y) add(3,5)
在函數執行前,打印一行日誌do...
;函數執行結束,打印一行日誌finish
。執行結果以下:app
do add 3 + 5 = 8 finish
計算時間函數
import time def timer(func): def wrapper(*args, **kw): t1 = time.time() func(*args,**kw) t2 = time.time() cost_time = t2 - t1 print 'cost time: {} s'.format(cost_time) return wrapper @timer def cost_time(sleep_time): time.sleep(sleep_time) cost_time(10)
def say_hello(country): def wrapper(func): def decorate(*args,**kw): if country == 'en': print 'hello' elif country == 'usa': print 'hi' else: return func(*args,**kw) return decorate return wrapper @say_hello("usa") def usa(): print 'i am from usa' @say_hello("en") def en(): print 'i am from england' usa() print '----------------------' en()
裝飾器自己是一個函數,使用兩層嵌套傳參,執行結果以下:日誌
hi i am from usa ---------------------- hello i am from england
__call__
和__init__
兩個內置函數。__init__
:接收被裝飾函數__call__
:實現裝飾邏輯class logger(object): def __init__(self,func): self.func = func def __call__(self,*args,**kwargs): print 'the function {}() is running...'\ .format(self.func.__name__) return self.func(*args,**kwargs) @logger def say(something): print 'say {}!'.format(something) say('hello')
運行結果以下:code
the function say() is running... say hello!
__init__
:再也不接收被裝飾函數,而是接收傳入參數__call__
:接收被裝飾函數,實現裝飾邏輯class logger(object): def __init__(self,level='INFO'): self.level = level def __call__(self,func): def wrapper(*args,**kwargs): print '{level}: the function {func} () is running...'\ .format(level=self.level, func=func.__name__) func(*args,**kwargs) return wrapper @logger(level='WARNING') def say(something): print 'say {}!'.format(something) say('hello')
運行結果以下:orm
WARNING: the function say () is running... say hello!
def power(x, n): s = 1 while n > 0: n = n - 1 s = s * x return s
power(x, n)
函數有兩個參數:x
和n
,這兩個參數都是位置參數,調用函數時,傳入的兩個值按照位置順序依次賦值給參數x
和n
。ci
def power(x, n=2): s = 1 while n > 0: n = n - 1 s = s * x return s
power(x, n)
函數有兩個參數:x
和n
,若是想在不傳入n
值時,默認計算x
的平方,此時能夠將n
設爲默認值2。it
def function(f_arg, *args): print f_arg, type(f_arg) print args, type(args) nums = ['a','b','c'] function(1,2,*nums)
定義可變參數時,須要在參數前面加一個*
號,可變參數的個數是可變的。在函數內部,參數*args
接收到的是一個tuple
。輸出結果以下:io
1 <type 'int'> (2, 'a', 'b', 'c') <type 'tuple'>
def person(name,age,**kwargs): print 'name:',name,'age:',age,'other:',kwargs,type(kwargs) person('mark',30,city='shanghai')
**kwargs
容許將不定長度的鍵值對,做爲參數傳遞給一個函數,關鍵字參數在函數內部自動組裝爲一個dict
。輸出結果以下:function
name: mark age: 30 other: {'city': 'shanghai'} <type 'dict'>
def hi(): return 'hi friends' def function(func): print 'just test' print func() function(hi)
function()
函數將hi
函數做爲參數接收,輸出結果以下:
just test hi friends
>>> time.localtime() time.struct_time(tm_year=2019, tm_mon=8, tm_mday=21, tm_hour=14, tm_min=31, tm_sec=18, tm_wday=2, tm_yday=233, tm_isdst=0)
>>> time.ctime() 'Wed Aug 21 14:51:28 2019' >>> time.asctime() 'Wed Aug 21 14:51:34 2019'
>>> time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()) '2019-08-21 14:35:02' >>> time.strftime('%a %b %d %H:%M:%S %Y',time.localtime()) 'Wed Aug 21 14:36:09 2019'
import time start = time.time() time.sleep(2) end = time.time() print end-start