#! /usr/bin/env python # coding=utf-8 from time import time def logged(when): def log(f,*args,**kargs): print("called: function:%s,args:%r,kargs:%r"%(f,args,kargs)) def pre_logged(f): def wrapper(*args,**kargs): log(f,*args,**kargs) return f(*args,**kargs) def post_logged(f): def wrapped(*args,**kargs): now=time() try: return f(*args,**kargs) finally: log(f,*args,**kargs) print("time delta:%s"%(time()-now)) return wrapped try: #從這裡開始呼叫 return{"pre":pre_logged,"post":post_logged}[when] except Exception as e: print(e) @logged("post") def hello(name): print("hello",name) @logged("post") def test(a,b=1): print(a+b) hello("world") test(1,2)
用python 裝飾器打log
相關文章
- python裝飾器2:類裝飾器Python
- Python裝飾器探究——裝飾器引數Python
- Python 裝飾器你也會用Python
- python裝飾器decorator的應用Python
- Python 裝飾器Python
- Python裝飾器Python
- 裝飾器 pythonPython
- [Python小記] 裝飾器怎麼用 ?Python
- Python 裝飾器裝飾類中的方法Python
- Python裝飾器模式Python模式
- python的裝飾器Python
- 1.5.3 Python裝飾器Python
- Python 裝飾器(一)Python
- Python 裝飾器原理Python
- 草根學Python(十六) 裝飾器(逐步演化成裝飾器)Python
- python 之裝飾器(decorator)Python
- Python深入05 裝飾器Python
- Python裝飾器詳解Python
- Python中的裝飾器Python
- 【Python】淺談裝飾器Python
- 初識Python裝飾器Python
- Python 裝飾器的理解Python
- python裝飾器介紹Python
- 淺談Python裝飾器Python
- 用Python functools.wraps實現裝飾器Python
- Python3 裝飾器解析Python
- Python 語法之裝飾器Python
- Python裝飾器高階用法Python
- python裝飾器入門探究Python
- python 裝飾器 part2Python
- python裝飾器有哪些作用Python
- python裝飾器是什麼Python
- Python 裝飾器簡單示例Python
- 粗淺聊聊Python裝飾器Python
- python中裝飾器的原理Python
- Python閉包與裝飾器Python
- Python之函式裝飾器Python函式
- Python深入分享之裝飾器Python