Python裝飾器是一個消除冗余的強大工具。隨著將功能模塊化為大小合適的方法,即使是最復雜的工作流,裝飾器也能使它變成簡潔的功能。
例如讓我們看看Django web框架,該框架處理請求的方法接收一個方法對象,返回一個響應對象:
def handle_request(request): return HttpResponse("Hello, World")
我最近遇到一個案例,需要編寫幾個滿足下述條件的api方法:
返回json響應 如果是GET請求,那么返回錯誤碼做為一個注冊api端點例子,我將會像這樣編寫:
def register(request): result = None # check for post only if request.method != 'POST': result = {"error": "this method only accepts posts!"} else: try: user = User.objects.create_user(request.POST['username'], request.POST['email'], request.POST['password']) # optional fields for field in ['first_name', 'last_name']: if field in request.POST: setattr(user, field, request.POST[field]) user.save() result = {"success": True} except KeyError as e: result = {"error": str(e) } response = HttpResponse(json.dumps(result)) if "error" in result: response.status_code = 500 return response
然而這樣我將會在每個api方法中編寫json響應和錯誤返回的代碼。這將會導致大量的邏輯重復。所以讓我們嘗試用裝飾器實現DRY原則吧。
裝飾器簡介
如果你不熟悉裝飾器,我可以簡單解釋一下,實際上裝飾器就是有效的函數包裝器,python解釋器加載函數的時候就會執行包裝器,包裝器可以修改函數的接收參數和返回值。舉例來說,如果我想要總是返回比實際返回值大一的整數結果,我可以這樣寫裝飾器:
# a decorator receives the method it's wrapping as a variable 'f'def increment(f): # we use arbitrary args and keywords to # ensure we grab all the input arguments. def wrapped_f(*args, **kw): # note we call f against the variables passed into the wrapper, # and cast the result to an int and increment . return int(f(*args, **kw)) + 1 return wrapped_f # the wrapped function gets returned.
現在我們就可以用@符號和這個裝飾器去裝飾另外一個函數了:
@incrementdef plus(a, b): return a + b result = plus(4, 6)assert(result == 11, "We wrote our decorator wrong!")
裝飾器修改了存在的函數,將裝飾器返回的結果賦值給了變量。在這個例子中,'plus'的結果實際指向increment(plus)的結果。
對于非post請求返回錯誤
現在讓我們在一些更有用的場景下應用裝飾器。如果在django中接收的不是POST請求,我們用裝飾器返回一個錯誤響應。
新聞熱點
疑難解答