Python Decorator
2018-07-21 本文已影响0人
revlis
利用装饰器记录函数运行时间
import datetime
import time
from functools import reduce
class Decorator():
@staticmethod
def exectime(func):
def decor(*arg):
start = datetime.datetime.now()
ret = func(*arg)
print("{} = {}".format(func.__name__, ret))
end = datetime.datetime.now()
interval = end - start
print("{} execution time: {}".format(func.__name__, interval))
return ret
return decor
class Util():
@staticmethod
# @Decorator.exectime
def sum(*arg):
# slotion 1
# s=0
# for i in arg:
# # time.sleep(1)
# s+=i
# solution 2
# head, *remain = arg
# if remain is None:
# return 0
# return header + sum(remain)
# solution 3
return reduce(lambda x,y:x+y, arg)
Util.sum(1,2,3)
>> sum = 6
>> sum execution time: 0:00:03.012014
list去空
s=['A', '', 'B', None, 'C', ' ']
s=list(filter(lambda x:x and x.strip(), s))
if x and y or z 等于三元表达式 x?y:z