Python annotator learning notes

Source: Internet
Author: User
Tags python decorator

Python annotator learning notes
What is a python decorator? The decorator is actually a function, a function used to wrap the function, returns a modified function object, and assigns it the original identifier again, and permanently loses access to the original function object. Eg: to add the same functions in Func1 and Func2, you can add all functions in outer once. You can establish a connection between the decorator and the function by using the @ + decorator name in the first line of the function. In addition, you must return the decorated object in the decorator.

Def outer (fun): def wrapper (): print 'Authentication' fun () print 'zhuangshiq' return wrapper # the object of the decorator @ outer # The decorator establishes a connection with the function def Func1 (): print 'func1 '@ outerdef Func2 (): print 'func2' Func1 () func2 ()

 

Decorator parameters: If the function contains accepted parameters, you must add a parameter to the decorator. In addition, you must add a function call to the function call inside the decorator.
Def outer (fun): def wrapper (arg): # arg is the form parameter print 'to verify 'print arg fun (arg) print 'zhuangshiq' return wrapper @ outer # The decorator establishes a connection with the function def Func1 (arg): print 'func1', arg @ outerdef Func2 (arg): print 'func2 ', arg Func1 ('A') Func2 ('A ')

 

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.