Python Learning Series Python Decorator Basics (2)---decorating functions with return values

Source: Internet
Author: User
Tags python decorator

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.