Python Decorator Learning Notes

Source: Internet
Author: User

What is a python adorner?

An adorner is actually a function, a function that wraps a function, returns a modified function object, assigns it the original identifier, and permanently loses access to the original function object.

eg: when you need to add the same functionality to FUNC1 and FUNC2, you can add the entire function once in the outer. The way the adorner connects to a function is done with the @+ adorner name in the previous line of the function . And be sure to return the decorated object in the adorner.

def outer (fun): Def wrapper (): print ' verify ' fun () print ' Zhuangshiq ' return wrapper# be sure to return an adorner object @outer #装饰器与函数建立连接def Func1 (): print ' func1 ' @outerdef Func2 (): print ' Func2 ' Func1 () Func2 ()

Adorner parameters:

If there are accepted arguments in the function, you must add a parameter to the adorner. A function call is also added to the function call inside the adorner.

def outer (fun): Def wrapper (ARG): #arg为形参 print ' verify ' Print arg fun (arg) print ' Zhuangshiq '    Return wrapper @outer #装饰器与函数建立连接def Func1 (ARG): print ' func1 ', Arg@outerdef Func2 (ARG): print ' Func2 ', arg Func1 (' a ') Func2 (' a ')


Python Decorator Learning Notes

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.