python的装饰器案例
2023-12-18 python 494
import time def record_time(fn): end = "ms" def func(m): start = time.time() fn(m) print("%f %s" % (time.time() - start, end)) return func def logging(end="ms"): print("in logging") def decorator(fn): print("in decorator") def inner(m): print("in inner") start = time.time() fn(m) print("%f %s" % (time.time() - start, end)) return inner return decorator @logging('毫秒') def test(m): for i in range(1000000): i += 1 print("test => %s" % m) @record_time def test2(m): for i in range(1000000): i += 1 print("test2 => %s" % m) def main(): test("test param") test2("test2 param") if __name__ == "__main__": main()
很赞哦! (0)
文章评论
-
-
-
0条评论