In the previous article (1), I wrote an adorner and how to use it, but if you encounter a function with a return value, it is not good to make, so rewrite an adorner as follows:
#basic2. Py#coding:utf-8def Auth (func): Def inner (*arg, **kwargs): print ' before ' temp = func (*arg,**kwarg s) #这行是关键, func equals fetch_server_list print ' after ' return temp #相当于返回fetch_server_list的值, and because the original function fetch_server_ The value of the list is equivalent to returning the value of server_list, so the end result is [' A1 ', ' A2 ', ' A3 ']. return inner@authdef fetch_server_list (ARG): serve_list = [' A1 ', ' A2 ', ' A3 '] return Serve_list
Call this adorner:
#b2. Pyimport basic2ret_list = basic2.fetch_server_list (' Test ') print Ret_list
Execution Result:
#python b2.pybeforeafter[' A1 ', ' A2 ', ' A3 ']
This article is from the "bitter coffee ' s Yun ' an Road" blog, please be sure to keep this source http://alsww.blog.51cto.com/2001924/1718015
Python Learning Series Python Decorator Basics (2)---decorating functions with return values